为什么在重新创建活动时我的savedInstanceState为空?

时间:2014-04-23 03:38:54

标签: android android-activity android-lifecycle android-navigation

我仔细阅读了活动生命周期Android文档,我认为我这样做是正确的,但它不起作用。我用courseId来生成Activity_Course.class的内容。当我单击此活动的ListView中的项目时,它会转到另一个活动。当我向上导航到Activity_Course时,我想保持courseId,以便我可以再次给屏幕充气。

导航到新活动时,我的onSaveInstanceSTate()覆盖似乎按预期工作。但是,当我返回到原始活动时,onCreate()声明它收到的savedInstanceState为null。我错过了什么?

第一个也是最后一个方法是我的重点,但我已将所有方法都包括在内。

public class Activity_Course extends ActionBarActivity {

    static final String COURSE_ID = "Activity_Course.COURSE_ID";
    public int courseId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.course_activity_frame, new CourseActivityFragment()).commit();
            Intent intent = getIntent();
            courseId = intent.getIntExtra(MainFragment.COURSE_ID, 0);
            System.out.println("the saved state was null");  // This prints!
        } else {
            courseId = savedInstanceState.getInt(COURSE_ID);
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.course, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public static class CourseActivityFragment extends Fragment {

        View rootView;
        int courseId;
        DBAdapter db;
        ListView GradeTypeListView;
        Adapter_GradeTypeList gtAdapter;
        public final static String CATEGORY_ID = "MY_CATEGORY_ID";

        public ArrayList<Category> gradeTypeList = new ArrayList<Category>();

        public CourseActivityFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            rootView = inflater.inflate(R.layout.activity_course_fragment,
                    container, false);

            db = new DBAdapter(this.getActivity());
            courseId = ((Activity_Course) this.getActivity()).courseId;
            db.open();
            Course course = db.getCourse(courseId);
            db.close();

            TextView header = (TextView)rootView.findViewById(R.id.course_header);
            ((TextView) rootView.findViewById(R.id.course_semester)).setText(course.getSemesterString());
            header.setText(course.getTitle());

            LinearLayout catHeader = (LinearLayout) rootView.findViewById(R.id.linLayCatheader);
            TextView tv = (TextView)catHeader.findViewById(R.id.category_column);
            TextView tv2 = (TextView)catHeader.findViewById(R.id.weight_column);
            TextView tv3 = (TextView)catHeader.findViewById(R.id.gradecount_column);
            TextView tv4 = (TextView)catHeader.findViewById(R.id.percentage_column);
            tv.setText(R.string.cat_header);
            tv2.setText(R.string.weight_header);
            tv3.setText(R.string.count_header);
            tv4.setText(R.string.percent_header);

            setListData();

            Resources res = getResources();
            GradeTypeListView = (ListView) rootView.findViewById(R.id.gtListView);

            gtAdapter = new Adapter_GradeTypeList(this.getActivity(), gradeTypeList, res, this);
            GradeTypeListView.setAdapter(gtAdapter);

            System.out.println("how many?   " + GradeTypeListView.getChildCount());

            View v = GradeTypeListView.getChildAt(2);

            return rootView;
        }

        public void setListData()
        {
            db.open();
            Category[] gt = db.getCategoriesInCourse(courseId);
            db.close();
            if(gt!=null){
                for(int i = 0; i < gt.length; i++) {
                    gradeTypeList.add(gt[i]);
                }
            }
        }

         public void onItemClick(int position){
             System.out.println(gradeTypeList.get(position).getType());
             System.out.println("taken from adapter is, title is..   " + gtAdapter.getItem(position).getType());

                Intent i = new Intent(this.getActivity(), Activity_Category.class);
                i.putExtra(CATEGORY_ID, gtAdapter.getItem(position).getCourseId());
                i.putExtra(MainFragment.COURSE_ID, courseId);
                startActivity(i);
            }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState){
        savedInstanceState.putInt(COURSE_ID, courseId);
        // this print shows correctly!
        System.out.println("courseId I'm saving is... " + courseId); 
        super.onSaveInstanceState(savedInstanceState);
    }
}

2 个答案:

答案 0 :(得分:0)

您在片段中的onCreate中缺少setRetainInstance(true)

答案 1 :(得分:0)

我在另一个问题上找到了这个答案。这正是我想要的。 Returning from an activity using navigateUpFromSameTask()