savedInstanceState从另一个活动返回null

时间:2015-10-01 05:17:56

标签: android android-fragments

我有一个问题,即在按回按钮以返回原始活动时,片段的oncreate方法中保存的实例状态为null。如果屏幕的方向改变,即savedinstance状态不为空,则相同的代码完全正常。我是android的新手所以请帮助。

我的片段代码:

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    if(savedInstanceState != null) {
        Log.v(LOG_TAG, "Movie Detail Fragment - Saved Instance state is not null");
        detailMovie = savedInstanceState.getParcelableArrayList(MOVIE_KEY);
    } else {
        Log.v(LOG_TAG, "Movie Detail Fragment - oncreate - saved instace state is null");
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    Log.v(LOG_TAG, "Movie Detail Fragment - onSaveInstanceState is called");
    if(movieDetails != null){
        detailMovie = new ArrayList<MovieDetail>();
        detailMovie.add(new MovieDetail(MOVIE_KEY, movieDetails));
        outState.putParcelableArrayList(MOVIE_KEY, (ArrayList<? extends Parcelable>) detailMovie);
        Log.v(LOG_TAG, "Movie Detail Fragment - detail movie is not null - " +movieDetails.length);
    } else {
        Log.v(LOG_TAG, "Movie Detail Fragment - detail movie is null");
    }
    super.onSaveInstanceState(outState);
}

我的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/movie_detail_list"
    android:editable="false" />

</LinearLayout>

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    Log.v(LOG_TAG1, "CustomDetailAdapter - getView");
    if (convertView == null) {
        Log.v(LOG_TAG1, "CustomDetailAdapter - getView - convertview null");
        inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(R.layout.movie_details, parent, false);
    }
    final String[] values = details.get(position);
    Log.v(LOG_TAG1, "CustomDetailAdapter - getView - convertview is not null. Position - " +position);
    ImageView imageView = (ImageView) convertView.findViewById(R.id.movie_pic);
    TextView textView1 = (TextView) convertView.findViewById(R.id.movie_details_textview1);
    TextView textView2 = (TextView) convertView.findViewById(R.id.movie_details_textview2);
    TextView textView3 = (TextView) convertView.findViewById(R.id.movie_details_textview3);
    TextView textView4 = (TextView) convertView.findViewById(R.id.movie_details_textview4);
    Button buttonView =  (Button) convertView.findViewById(R.id.movie_details_reviews);
    buttonView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Log.v(LOG_TAG1, "CustomDetailAdapter - getView - item: Hello Click");
            if(context instanceof DetailActivity){
                Log.v(LOG_TAG1, "CustomDetailAdapter - instance of detail activity");
            }
            Intent intent = new Intent(context, MovieReviewerActivity.class);
            intent.putExtra("moviedetails", values);
            context.startActivity(intent);
            Log.v(LOG_TAG1, "Movie Detail Fragment - CustomDetailAdapter - starting review");
        }
    });
    if (values != null) {
        Log.v(LOG_TAG1, "CustomDetailAdapter - values is not null");
        Picasso
                .with(context)
                .load(values[0])
                .fit()
                .into(imageView);
        textView1.setText(values[1]);
        textView2.setText(values[2]);
        textView3.setText(values[3]);
        textView4.setText("Rating: " + values[4]);
        buttonView.setText("Reviews");
    } else {
        Log.v(LOG_TAG1, "CustomDetailAdapter - values is null");
    }
    return convertView;
}

1 个答案:

答案 0 :(得分:0)

只有当系统杀死您的活动或片段时,才会调用onSaveInstanceState,当您重新创建活动或片段时,系统会将您放入onSaveInstanceState的内容传递给onCreate(Bundle),因此,如果您从Acticity A或Fragment A跳转到ActivityB或FragmentB,则不会调用onSaveInstanceState方法,那么onCreate中的额外参数将为null。 接下来是为什么“如果屏幕的方向改变,即savedinstance状态不为空。”,那是因为当你的屏幕方向改变时,你的Activity将被系统杀死并重新创建(如果你没有设置android),所以你应该知道为什么savedInstance不为空。