Android onActivityForResult 如何反应在 foregrnd 和 bckgrnd 之间切换应用程序?

发布于 2024-10-26 06:44:12 字数 1153 浏览 1 评论 0原文

考虑这种情况:

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();
     }

      ....
   }
 }
}

步骤:

  1. 启动父级活动。
  2. 单击父级上的按钮 b1 并启动子级
  3. 子级已成功引入并显示在屏幕上
  4. 现在点击 Android 设备(硬件)上的 Home 按钮
  5. 调用子级的 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:

  1. Launch Parent activity.
  2. Click Button b1 on Parent and launch child
  3. Child is brought successfully and shows up on screen
  4. Now hit Home Button on the Android device (hardware)
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

记忆之渊 2024-11-02 06:44:12

如果您在 onPause 中删除对 finish() 的调用,则按 home 键将仅调用子活动的 onPause 方法。当您重新启动应用程序时,仅调用子活动的 onResume 方法。似乎没有调用任何父方法。用户更改手机方向后,在 Activity 中调用 setResult 然后在 onPause 中完成的模式会失败。因此,如果用户输入数据,保存数据(在 onSaveButtonClicked 中调用 setResult),更改手机方向,然后退出调用完成,并且更改不会保存。

当您有足够的数据来设置结果时,请考虑在 setResult 之后立即调用 finish() ,然后退出子活动。

// UPDATE BUTTON HANDLER
    final Button buttonUpdate= (Button)findViewById(R.id.ButtonPasswordUpdate);
    buttonUpdate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {   
            String password= editTextPasswordFirst.getText().toString();
            String verify= editTextPasswordSecond.getText().toString();  
            String err="";

            if (password.equals(verify) && (password.length() >= minimumPasswordLength)) { // 1) SUCCESS
                ResetTimeoutValues(timeoutType); // new password so calculate new timeout time     
                isValidKey= true;
                PasswordState outPasswordState= new PasswordState(lengthKey,
                        timeExpire,
                        isValidKey,
                        timeoutType,
                        password,
                        isHashPassword,
                        minimumPasswordLength);
                Bundle b= new Bundle();
                b.putSerializable("jalcomputing.confusetext.PasswordState", outPasswordState);
                getIntent().putExtras(b);
                setResult(RESULT_OK,getIntent());   // call home with data on success only
                finish(); // go back <=== EXITS Here
                return;
            }...
    });

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.

// UPDATE BUTTON HANDLER
    final Button buttonUpdate= (Button)findViewById(R.id.ButtonPasswordUpdate);
    buttonUpdate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {   
            String password= editTextPasswordFirst.getText().toString();
            String verify= editTextPasswordSecond.getText().toString();  
            String err="";

            if (password.equals(verify) && (password.length() >= minimumPasswordLength)) { // 1) SUCCESS
                ResetTimeoutValues(timeoutType); // new password so calculate new timeout time     
                isValidKey= true;
                PasswordState outPasswordState= new PasswordState(lengthKey,
                        timeExpire,
                        isValidKey,
                        timeoutType,
                        password,
                        isHashPassword,
                        minimumPasswordLength);
                Bundle b= new Bundle();
                b.putSerializable("jalcomputing.confusetext.PasswordState", outPasswordState);
                getIntent().putExtras(b);
                setResult(RESULT_OK,getIntent());   // call home with data on success only
                finish(); // go back <=== EXITS Here
                return;
            }...
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文