本文介绍了为什么不活动的onDestroy一个没有被称为一个电话后完成()在ActivityB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

接下来的过程简单理解和再现,但导致错误:

The next process is simple to understand and to reproduce but leads to a bug:

  • activityA开始在其的onCreate()方法
  • 的activityB
  • activityB被创建,我称之为完成()在onResume()方法
  • activityB 的onDestroy()
  • activityA onResume()
  • ,并在这里activityA,我点击菜单按钮来调用完成() - 或preSS返回键
  • activityA被删除,但的onDestroy()不叫,并且A还活着(亚行外壳dumpsysmyPackageName表示太多的生活活动)
  • activityA starts an activityB in its onCreate() method
  • activityB is created and I call finish() in its onResume() method
  • activityB onDestroy() is called
  • activityA onResume() is called
  • and here in activityA, I click a menu button to call finish() - or press the back key.
  • activityA is removed but onDestroy() is NOT called and A is still living ( adb shell dumpsys 'myPackageName' indicates too many living Activities )

code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="gleroy.com.algo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".activity.MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <activity
            android:name="gleroy.com.algo.activity.FakeA"
            android:label="@string/app_name"></activity>

        <activity
            android:name="gleroy.com.algo.activity.FakeB"
            android:label="@string/app_name"></activity>
    </application>
</manifest>

活动答:

public class FakeA extends Activity {

    private final static String TAG = FakeA.class.getCanonicalName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate, taskId :" + getTaskId());
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(FakeA.this, FakeB.class);
        startActivity(intent);
    }

    @Override
    protected void onResume() {
        Log.d(TAG, "onResume");
        super.onResume();
    }


    @Override
    protected void onDestroy() {
        Log.d(TAG, "onDestroy");
        super.onDestroy();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        super.onCreateOptionsMenu(menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.stop_session_menu_item:
                /* stop and quit */
                finish();
                return false;
        }

        return super.onOptionsItemSelected(item);
    }
}

活动B:

public class FakeB extends Activity {

    private final static String TAG = FakeB.class.getCanonicalName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate, taskId :"+getTaskId());
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume, isFinishing :" + isFinishing());
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }
}

活动A从MainActivity开始,其中包含一个简单的按钮:

Activity A is started from MainActivity which contains a simple button :

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, FakeA.class);
            startActivity(intent);
        }
    });

所以,我知道,我们不能保证的onDestroy()是要去被称为但是我在这里的ActivityA显然泄漏。

So I know that we can't be sure that onDestroy() is gonna be called but here my ActivityA is clearly leaking.

此外,我认为,如果我使用定时器的TimerTask 延迟 startActivity 在ActivityA或完成()在ActivityB话,我就没有这个错误了。

Also I observed that if I use a Timer and TimerTask to delay startActivity in ActivityA or finish() in ActivityB then I don't have this bug anymore.

下面是事件:

  • FakeA的onCreate,TASKID:154
  • FakeA onResume
  • FakeA的onPause,isFinishing:假
  • FakeB的onCreate,TASKID:154
  • FakeB onResume,isFinishing:假
  • FakeA onResume
  • FakeB的onDestroy
  • 在通话结束或preSS返回键:FakeA的onPause,isFinishing:真正的

推荐答案

在地方的结束()尝试 finishAffinity()。我所知道的:完成()只是破坏当前动态活动同时, finishAffinity()摧毁所有活动活动

In place of finish() try finishAffinity().As far as i know:finish() just destroys the current live Activitywhile, finishAffinity() destroys all active Activities.

这篇关于为什么不活动的onDestroy一个没有被称为一个电话后完成()在ActivityB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 19:37