简体   繁体   English

Android:通话结束后将重新创建活动

[英]Android: activity will be recreated after calling finish

I'm trying to create an app which contains two activities: MainActivity and SubActivity . 我正在尝试创建一个包含两个活动的应用程序: MainActivitySubActivity

SubActivity will be created from MainActivity by calling startActivityForResult() . 通过调用startActivityForResult()MainActivity创建SubActivity

When I tap the button settled in SubActivity , it should finish itself and go to MainActivity . 当我点击SubActivity的按钮时,它应该自动完成并转到MainActivity But I get problem with finishing SubActivity . 但是我在完成SubActivity遇到问题。 After SubActivity.onPause() is called, SubActivity.onCreate() will be called immedietly and the SubActivity will be recreated. 之后SubActivity.onPause()被调用, SubActivity.onCreate()将immedietly叫和子活动将被重新创建。

I just wanna close SubActivity and display MainActivity , but why is this happen? 我只想关闭SubActivity并显示MainActivity ,但是为什么会这样呢?

I searched for same questions and found that rotation change will make the system call onCreate(). 我搜索了相同的问题,发现轮换更改将使系统调用onCreate()。 But in my case, the screen orientation fixed to portrait. 但就我而言,屏幕方向固定为纵向。

Could someone please give me an idea to solve this problem? 有人可以给我一个解决这个问题的想法吗?

EDIT: I solved the problem. 编辑:我解决了问题。 There was a wrong logic in the MainActivity . MainActivity逻辑有误。

SubActivity will be created from MainActivity , when a variable holds a specific value. 当变量具有特定值时,将从MainActivity创建SubActivity The display update method which reflects the value of the variable was called twice in code. 在代码中两次调用了反映变量值的显示更新方法。 That brought my App to recreate the SubActvity after finish() . 这使我的App在finish()之后重新创建SubActvity

MainActivity requests to create Subactivity twice, but since my app is set as android:launchMode="singleTop" , the second SubActivity could be only created after the first one has been finished. MainActivity请求两次创建Subactivity ,但是由于我的应用程序设置为android:launchMode="singleTop" ,因此只有在第一个SubActivity完成后才能创建第二个SubActivity Thanks a lot to give me advice! 非常感谢给我建议!

You can use or first check the behaviour of, 您可以使用或先检查的行为,

1) finishAffinity(); 1) finishAffinity();

2) finishActivityFromChild(); 2) finishActivityFromChild();

May it work. 祝它工作。

Otherwise for better communication put some line of code here so we will understand proper and give some guidance to you. 否则,为了更好地进行交流,请在此处添加一些代码行,以便我们正确理解并为您提供一些指导。

In my case, I called below functions. 就我而言,我调用了以下函数。

btn.setOnClickListener {
    setResult(result)
    finishActivity(REQUEST_CODE)
    finish()
}

As you describe I am giving you example. 正如你所描述的,我给你举个例子。 From MainActivity call the SubActivity using startActivityForResult() method MainActivity ,使用startActivityForResult()方法调用SubActivity

For example: 例如:

Intent intent = new Intent(this, SubActivity.class);
startActivityForResult(intent, 1);

Now in your SubActivity set the data which you want to send back to MainActivity . 现在,在SubActivity设置要发送回MainActivity (Below is both example) (下面都是例子)

With data example: 带有数据示例:

Intent intent = new Intent();
intent.putExtra("key",result);
setResult(Activity.RESULT_OK, returnIntent);
finish();

No data example: (If you don't have data send to MainActivity ) 无数据示例:(如果您没有将数据发送到MainActivity

Intent intent = new Intent();
setResult(Activity.RESULT_CANCELED, intent);
finish();

In your MainActivity Override onActivityResult() method to retrieve result. 在您的MainActivity Override onActivityResult()方法中检索结果。 (Only if you send data from SubActivity ) (仅当您从SubActivity发送数据时)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1) {
        if(resultCode == Activity.RESULT_OK){
            String data=data.getStringExtra("key");
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            //Write your code if there's no result
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 活动在调用finish()之后重新创建 - Activity is recreated after called finish() on it android 活动在调用完成()后继续运行 - android activity keep running after calling finish() Android:在旋转重新启动活动后对活动调用完成() - Android: Calling finish() on activity after rotating restarts activity 活动在finish()之后不调用onDestroy() - activity not calling onDestroy() after finish() Android 应用程序在活动中调用完成后进入后台 - Android application goes in background after calling finish in an activity 为什么在调用finish()之后不停止android活动? - Why doesn't stop an android activity after calling finish()? 在子活动中的onPause中调用finish(),以便用户重新关注父活动。 而是重新创建子活动 - Calling finish() in onPause in child activity so that user refocuses into parent activity. Child activity gets recreated instead 销毁活动后重新创建Android服务 - Android Service is recreated after destroying activity Android Studio:在活动中调用“finish()”后,前一屏幕中的代码无法继续 - Android Studio: Code in previous screen not continuing after calling “finish()” in activity 调用finish()时不会破坏android活动 - android activity is not destroyed on calling of finish()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM