gpt4 book ai didi

android - 为什么在重新创建 Activity 时我的 savedInstanceState 为 Null?

转载 作者:太空狗 更新时间:2023-10-29 15:04:36 25 4
gpt4 key购买 nike

我通读了 Android Activity 生命周期文档,我认为我这样做是对的,但它不起作用。有一个 courseId,我用它来生成我的 Activity_Course.class 的内容。当我单击此 Activity 的 ListView 中的项目时,它会将我带到另一个 Activity 。当我导航回“向上”到 Activity_Course 时,我想维护那个 courseId 以便我可以再次使屏幕膨胀。

我的 onSaveInstanceSTate() 覆盖在导航到新 Activity 时似乎按预期工作。但是当我返回到原始 Activity 时,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);
}
}

最佳答案

您在 fragment 的 onCreate 中缺少 setRetainInstance(true)

关于android - 为什么在重新创建 Activity 时我的 savedInstanceState 为 Null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23234295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com