重新创建我的活动时,为啥我的 savedInstanceState 为 Null?
Posted
技术标签:
【中文标题】重新创建我的活动时,为啥我的 savedInstanceState 为 Null?【英文标题】:Why is my savedInstanceState Null when recreating my activity?重新创建我的活动时,为什么我的 savedInstanceState 为 Null? 【发布时间】:2014-06-07 16:41:52 【问题描述】:我通读了 Activity 生命周期 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);
【问题讨论】:
【参考方案1】:您在片段的 onCreate 中缺少 setRetainInstance(true)
【讨论】:
【参考方案2】:我在另一个问题上找到了这个答案。这正是我想要的。 Returning from an activity using navigateUpFromSameTask()
【讨论】:
以上是关于重新创建我的活动时,为啥我的 savedInstanceState 为 Null?的主要内容,如果未能解决你的问题,请参考以下文章
为啥我在 Android 中杀死我的应用程序时服务会重新启动?
当我重新启动我的 kafka 代理时,为啥或如何丢失一些消息?