私のアプリでは、私が作成したデータベースからデータを読みます。回転とは別にすべてが正常に動作します。私は電話を回転させると例外はありませんが、Webサービスは再び実行され、私はそれを望んでいません。いくつかの研究の後で、私のモデルクラスがParcelableインターフェースを実装すべきであることを知りました。ここで私はGoogleのアンドロイドガイドサイトにしたものです。ParcelableクラスとAndroidのArrayList
public class FootballNewsObject implements Parcelable{
String title;
String article;
String image;
public FootballNewsObject() {
}
public FootballNewsObject(Parcel in) {
title = in.readString();
article = in.readString();
image = in.readString();
}
public void setArticle(String article) {
this.article = article;
}
public void setImage(String image) {
this.image = image;
}
public void setTitle(String title) {
this.title = title;
}
public String getArticle() {
return article;
}
public String getTitle() {
return title;
}
public String getImage() {
return image;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(title);
dest.writeString(article);
dest.writeString(image);
}
public static final Creator<FootballNewsObject> CREATOR = new Creator<FootballNewsObject>() {
@Override
public FootballNewsObject createFromParcel(Parcel in) {
Log.v("Theo","Create from parser: Movies");
return new FootballNewsObject(in);
}
@Override
public FootballNewsObject[] newArray(int size) {
return new FootballNewsObject[size];
}
};
}
もちろん、私は、ビューと実際のウェブサービスの世話をしている断片をVolley Libraryで使用しています。
public class FootballNews extends Fragment {
public static final String TAG = "ManuApp";
private static final String IMAGE_URL = "my link goes here" ;
private static final String FL_NEWS ="FL NEWS" ;
private List<FootballNewsObject> listItemsList;
private RecyclerView mRecyclerView;
private FootballNewsAdapter adapter;
public FootballNews() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
setRetainInstance(true);
View v = inflater.inflate(R.layout.fragment_football_news, container, false);
// Inflate the layout for this fragment
listItemsList = new ArrayList<FootballNewsObject>();
mRecyclerView = (RecyclerView)v.findViewById(R.id.recycler_view);
//mRecyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).color(Color.BLACK).build());
final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(linearLayoutManager);
if(savedInstanceState!=null){
listItemsList = savedInstanceState.getParcelableArrayList(FL_NEWS);
}
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
updateList();
}
public void updateList() {
listItemsList.clear();
//declare the adapter and attach it to the recyclerview
adapter = new FootballNewsAdapter(getActivity(), listItemsList);
mRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(getActivity());
// Clear the adapter because new data is being added from a new subreddit
//adapter.clearAdapter();
//showPD();
// Request a string response from the provided URL.
JsonArrayRequest jsObjRequest = new JsonArrayRequest(Request.Method.GET, Config.URL_FOOTBALL_NEWS, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
//hidePD();
// Parse json data.
// Declare the json objects that we need and then for loop through the children array.
// Do the json parse in a try catch block to catch the exceptions
try {
for (int i = 0; i < response.length(); i++) {
JSONObject post = response.getJSONObject(i);
FootballNewsObject item = new FootballNewsObject();
item.setTitle(post.getString("title"));
item.setImage(IMAGE_URL + post.getString("news_image"));
item.setArticle(post.getString("article"));
listItemsList.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
// Update list by notifying the adapter of changes
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
//hidePD();
}
});
jsObjRequest.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 50000;
}
@Override
public int getCurrentRetryCount() {
return 50000;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});
queue.add(jsObjRequest);
}
protected boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager)getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
return false;
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList(FL_NEWS, (ArrayList<? extends
Parcelable>) listItemsList);
}
}
あなたは私がParcelable
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList(FL_NEWS, (ArrayList<? extends
Parcelable>) listItemsList);
}
に私のリストのデータを格納しsavedInstanceがnullでない場合、単にデータを取得するために使用しています見ることができるように。
if(savedInstanceState!=null){
listItemsList = savedInstanceState.getParcelableArrayList(FL_NEWS);
}
しかし私が最初に言ったように、Webサービスは回転後に再び実行されています。私はそれをやめるために何をすべきですか?
ありがとうございました。
EDITは、私はこれを試してみましたが、それでも同じpropblem
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(gottaGettaList = (savedInstanceState==null)) {
updateList();
}else{
gottaGettaList=false;
}
}
私の編集コードをご覧ください。ローテーションはとても混乱するかもしれません:( – Theo
@Theo: 'updateList()'を完全にスキップすることはできませんが、それはあなたがリストをアダプターに入れているところです。 – CommonsWare
私はアダプタを持っている私の更新リストメソッドにifステートメントを置くことを意味していますか? – Theo