简体   繁体   中英

Android launchMode:singleTask override

I have two activities Main and SearchEvent activity. The Main activity has a launchmode:"singleTask" and there isn't any special parameter associated with SearchEvent activity.

When I click a button present in the Main activity, the SearchActivity activity starts in the foreground and displays a list of the events. When I click on one of the list items, the items should get the eventId and start the Main.class activity, but as the Main.class was already present in the stack it puts in on top but killing teh SearchActivity(I see that onDestroy method is called).

I don't want the onDestroy method of SearchEvent be called and just the SearchEvent activity goes in background and Main activity pops up.

Also if I try using launchMode="singleTop" it starts another instance of Main activity which I am not looking for.

What setting do I enable to start the same instance of Main and move the SearchEvent in the background?

Code in the manifest file.

MainActivity

<activity
        android:configChanges="orientation"
        android:launchMode="singleTask"
        android:name=".Main"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.CustomDialog" >
        <intent-filter >
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
        <intent-filter >
            <action android:name="android.intent.action.SEND" >
            </action>

            <category android:name="android.intent.category.DEFAULT" >
            </category>

            <data android:mimeType="image/*" >
            </data>
        </intent-filter>
        <intent-filter >
            <action android:name="android.intent.action.SEND" >
            </action>

            <category android:name="android.intent.category.DEFAULT" >
            </category>

            <data android:mimeType="video/*" >
            </data>
        </intent-filter>
    </activity>
    <activity>

SearchEvent code

<activity
            android:configChanges="orientation"
            android:label="@string/app_name"
            android:name="SearchEvent"
            android:screenOrientation="portrait"
            android:theme="@style/Theme.CustomDialog1" >
            <intent-filter >
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>

The code use to call the Main Activity for the first time.

Intent mainIntent = new Intent(SignIn.this, Main.class);
startActivity(mainIntent);

Code to call the SearchEvent from Main Activity

Intent searchActivity = new Intent(Main.this, SearchEvents.class);
startActivity(searchActivity);

Code to call again Main Activity from SearchEvents activity

Intent mainIntent = new Intent(SearchEvents.this, Main.class);
startActivity(mainIntent);

If the search activity is supposed to show the user a list of events and have the user select one and then call the main activity with this result, you probably want to do this in a different way.

If I understand this correctly, you probably want the main activity to start the search activity using startActivityForResult() and have the search activity just return the selected event to the main activity using setResult() when it completes. In this way the search activity doesn't have to start the main activity again, it just "goes back to it".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM