Android onActivityForResult 如何反应在 foregrnd 和 bckgrnd 之间切换应用程序?
考虑这种情况:
public class Parent extends Activity{
@Override
public void onCreate(Bundle icicle) {
super.onCreate();
.......
}
public void onResume(){
super.onResume();
....
onClick of BUTTON b1.....
Intent intent = new Intent(Parent.this, Child.class);
intent.putExtra("param", data);
startActivityForResult(intent, ACTION_LAUNCH_CHILD_ACTIVITY);
}
....
}
public void onActivityResult(){
do something......
}
}
}
============================================
public class Child extends Activity{
@Override
public void onCreate(Bundle icicle) {
super.onCreate();
.......
}
public void onPause(){
super.onPause();
setResult(OK);
finsh();
}
....
}
}
}
步骤:
- 启动父级活动。
- 单击父级上的按钮 b1 并启动子级
- 子级已成功引入并显示在屏幕上
- 现在点击 Android 设备(硬件)上的 Home 按钮
- 调用子级的 onPause、onStop 和 onDestroy。
但是当应用程序进入后台时,我没有看到程序控制进入Parent的onActivityResult。
当应用程序处于后台状态时,该特定程序的控制在哪里?
将应用程序带回前台时,活动生命周期的预期行为是什么?
提前致谢
任何人都可以解释一下吗?
Consider this scenario:
public class Parent extends Activity{
@Override
public void onCreate(Bundle icicle) {
super.onCreate();
.......
}
public void onResume(){
super.onResume();
....
onClick of BUTTON b1.....
Intent intent = new Intent(Parent.this, Child.class);
intent.putExtra("param", data);
startActivityForResult(intent, ACTION_LAUNCH_CHILD_ACTIVITY);
}
....
}
public void onActivityResult(){
do something......
}
}
}
==========================================
public class Child extends Activity{
@Override
public void onCreate(Bundle icicle) {
super.onCreate();
.......
}
public void onPause(){
super.onPause();
setResult(OK);
finsh();
}
....
}
}
}
Steps:
- Launch Parent activity.
- Click Button b1 on Parent and launch child
- Child is brought successfully and shows up on screen
- Now hit Home Button on the Android device (hardware)
- Child's onPause, onStop and onDestroy is called.
But I dont see the program control entering Parent's onActivityResult when app is brought to the backgrnd.
Where is the program control for this particular when app is in background status?
On bring app back to foregrnd what is the expected behavior in terms of activity life cycle?
Thanks in advance
Can anyone explain on this please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在 onPause 中删除对 finish() 的调用,则按 home 键将仅调用子活动的 onPause 方法。当您重新启动应用程序时,仅调用子活动的 onResume 方法。似乎没有调用任何父方法。用户更改手机方向后,在 Activity 中调用 setResult 然后在 onPause 中完成的模式会失败。因此,如果用户输入数据,保存数据(在 onSaveButtonClicked 中调用 setResult),更改手机方向,然后退出调用完成,并且更改不会保存。
当您有足够的数据来设置结果时,请考虑在 setResult 之后立即调用 finish() ,然后退出子活动。
If you remove the call to finish() in onPause, then hitting the home key calls only the child activities onPause method. When you re-launch the app, only the child activities onResume method is called. None of the parent methods seem to be called. The pattern of calling setResult in the activity and then finish in onPause FAILS after the user changes the phone orientation. So if the user enters data, saves the data (calling setResult in onSaveButtonClicked), changes phone orientation, and exits calling finish and the changes ARE NOT SAVED.
Consider calling finish() immediately after setResult at the moment you have enough data to set the result, exiting the child activity.