Question

See Update 2 for current Problem regarding of Activity Launch Mode

I am developing an Android app. Now I have the problem that once I open a new activity (that is not the activity with android.intent.action.MAIN and android.intent.category.LAUNCHER) in the app, and then press home button, and then press the app icon, the app does not navigate me back to the new activity I have already open (and shown on recent app list). Instead, it open a new MAIN activity.

What should I do to change this behaviour?

===================================================

Update

Code for Start Activity Other from Main Activity

    startActivity(new Intent(MainActivity.this, LocalActivity.class));

AndroidManifest.xml

    <activity
        android:name=".MainActivity"
        android:theme="@style/AppDrawTheme"
        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=".LocalActivity"
        android:launchMode="singleInstance"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:screenOrientation="portrait">
    </activity>

===================================================

Update 2

I solve the problem by change android:launchMode="singleInstance" to android:launchMode="singleTask"

Although I do not understand why singleInstance would be a problem. How should I understand what Google write on the Android document "except that the system doesn't launch any other activities into the task holding the instance" ?

Was it helpful?

Solution

Most probably you are setting Intent flags that cause this when you call startActivity() or you use activity's properties in AndroidManifest.xml that cause this. I would need to see your manifest or code to point where is the problem exactly.

Updated

Google docs also says:

The activity is always the single and only member of its task.

You should go through this page and read carefully about tasks and activities.

Basically, when you start your main activity it's created within new task. Next activities are placed in the same task(in the stack). Now, the crucial part, Android does not restore only single activity but entire task. This is default.

In case you use singleInstance every Activity has own Task and when you start app again(after HOME button) Android restore Task with root Activity, not the one with your last seen Activity. Be very careful when you change launchMode, if it's not clear how it works then you should probably find it out first, otherwise you may have more surprises in the future.

I hope it's clear now for you, if not please read linked documentation.

OTHER TIPS

You should use onPause() and onResume() in your activity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top