问题
I created an Android application with Two Fragment, MainFragment --> WebviewFragment. The MainFragment is GridView which fetch the Grid Item from Json link. When I navigate back from WebviewFragment to MainFragment by pressing the back button, the Gridview fetches the data again. Is there any solution for this?
MainFragment
public class MainFragment extends Fragment {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String url = "http://api....";
private ProgressDialog pDialog;
private List<Movie> movieList;
private GridView gridView;
private TextView scrollingtext;
private AppController appController;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
movieList = new ArrayList<Movie>();
View view = inflater.inflate(R.layout.main_fragment, container, false);
scrollingtext = (TextView) view.findViewById(R.id.scroll_annouc);
scrollingtext.setSelected(true);
gridView = (GridView) view.findViewById(R.id.Grid_view);
final CustomListAdapter adapter = new CustomListAdapter(getActivity(), movieList);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bundle bundle = new Bundle();
bundle.putString("LINK","http://www.kalerkantho.com/");
Fragment newFragment = new WebviewFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
newFragment.setArguments(bundle);
transaction.replace(R.id.main_fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage(" Loading ...");
pDialog.show();
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
appController.getInstance().addToRequestQueue(movieReq);
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
WebviewFragment
public class WebviewFragment extends Fragment {
WebView webView;
String newslink;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_webview,container,false);
newslink = getArguments().getString("LINK");
webView = (WebView) view.findViewById(R.id.webView);
webView.loadUrl(newslink);
webView.getSettings().setJavaScriptEnabled(true);
return view;
}
}
}
回答1:
You can use SharedPreferences to keep a flag of whether or not you already loaded the data and then check this flag whenever you switch between fragments.
You should also keep the data in either SharedPreferences as well if your flag is active or maintain an internal database and load that instead of transforming your JSON again.
回答2:
You can done this by using Fragment. Making your main content parent layout as Framelayout and inflate the Webview when pressing a button or what you want by using the command fragment.add and when backpress just use fragment.remove.
回答3:
inflated view wouldn't be null when you popback. so just inflate view when you replace Fragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (view ==null){//null means fragment replaced and not null means popbacked
View view = inflater.inflate(R.layout.main_fragment, container, false);
init();
}
return view;
}
private void init() {
scrollingtext = (TextView) view.findViewById(R.id.scroll_annouc);
scrollingtext.setSelected(true);
gridView = (GridView) view.findViewById(R.id.Grid_view);
final CustomListAdapter adapter = new CustomListAdapter(getActivity(), movieList);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Bundle bundle = new Bundle();
bundle.putString("LINK", "http://www.kalerkantho.com/");
Fragment newFragment = new WebviewFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
newFragment.setArguments(bundle);
transaction.replace(R.id.main_fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage(" Loading ...");
pDialog.show();
// Creating volley request obj
JsonArrayRequest movieReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
// adding movie to movies array
movieList.add(movie);
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
appController.getInstance().addToRequestQueue(movieReq);
}
回答4:
You can also use setRetainInstance(true); in MainFragment which will keep the instance state.
Hope this helps.
来源:https://stackoverflow.com/questions/35286800/how-to-stop-gridview-from-loading-again-when-i-press-back-button