Android activity recreate itself

后端 未结 13 1535
攒了一身酷
攒了一身酷 2021-02-05 07:06

My app normally works just fine, until I face a strange problem on specific device. There are 2 activities in App. After I start ActivityB inside of ActivityA, ActivityA starts

相关标签:
13条回答
  • 2021-02-05 07:28

    you can use android:launchMode="singleTop"in manifest.

    <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
    
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    
    0 讨论(0)
  • 2021-02-05 07:29

    You can try one thing provide your layout in onCreate() and do the rest of the work in onStart() ?? if it works??

    LIKE:

     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show);
       }
    

    and

          @Override
           protected void onStart() {
           // TODO Auto-generated method stub
        super.onStart();
        Log.i(TAG, "On Start .....");
    
          }
    

    See Activity Lifecycle enter image description here

    0 讨论(0)
  • 2021-02-05 07:31

    Override onStart() and onResume method in Activity A and check if the problem is still persist. and if possible please give your activtiy A and B code here.

    0 讨论(0)
  • 2021-02-05 07:32

    It too faced the exact problem and solved issue by Using android:launchMode="standard" in activity of manifest.

    0 讨论(0)
  • 2021-02-05 07:34

    The Activity documentation (http://developer.android.com/reference/android/app/Activity.html) says the following about the lifecycle of a background activity:

    A background activity (an activity that is not visible to the user and has been paused) is no longer critical, so the system may safely kill its process to reclaim memory for other foreground or visible processes. If its process needs to be killed, when the user navigates back to the activity (making it visible on the screen again), its onCreate(Bundle) method will be called with the savedInstanceState it had previously supplied in onSaveInstanceState(Bundle) so that it can restart itself in the same state as the user last left it.

    In other words, ActivityA may or may not be destroyed by the operating system while ActivityB is active, so this situation has to be handled in the code. If ActivityA has been destroyed, onCreate(Bundle) will be called, when the user presses the back button in ActivityB.

    0 讨论(0)
  • 2021-02-05 07:35

    There's an Android developer setting called "Do not keep activities". The description for this option is "Destroy every activity as soon as the user leaves it." This sounds like a good description of what you're seeing, and since you're only seeing it on a few phones the idea that this is caused by a non-default system setting seems plausible.

    Ideally your app would still work in this scenario, even if less optimally. But if this setting is a problem for your app, you may wish to document this problem for your users.

    0 讨论(0)
提交回复
热议问题