简体   繁体   English

Android launchMode:singleTask覆盖

[英]Android launchMode:singleTask override

I have two activities Main and SearchEvent activity. 我有两个活动Main和SearchEvent活动。 The Main activity has a launchmode:"singleTask" and there isn't any special parameter associated with SearchEvent activity. Main活动具有启动模式:“ singleTask”,并且没有与SearchEvent活动关联的任何特殊参数。

When I click a button present in the Main activity, the SearchActivity activity starts in the foreground and displays a list of the events. 当我单击Main活动中存在的按钮时,SearchActivity活动从前台开始并显示事件列表。 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). 当我单击列表项之一时,这些项应获取eventId并启动Main.class活动,但是由于Main.class已经存在于堆栈中,因此它放在顶部,但杀死了SearchActivity(我看到了onDestroy方法被调用)。

I don't want the onDestroy method of SearchEvent be called and just the SearchEvent activity goes in background and Main activity pops up. 我不希望SearchEvent的onDestroy方法被调用,而只是SearchEvent活动在后台运行并且Main活动弹出。

Also if I try using launchMode="singleTop" it starts another instance of Main activity which I am not looking for. 另外,如果我尝试使用launchMode =“ singleTop”,它将启动我没有寻找的Main活动的另一个实例。

What setting do I enable to start the same instance of Main and move the SearchEvent in the background? 我可以启用什么设置来启动Main的相同实例并在后台移动SearchEvent?

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 SearchEvent代码

<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. 该代码用于首次调用Main Activity。

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

Code to call the SearchEvent from Main Activity 从主活动调用SearchEvent的代码

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

Code to call again Main Activity from SearchEvents activity 代码以再次调用SearchEvents活动中的Main活动

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. 如果我正确理解这一点,则可能希望主活动使用startActivityForResult()启动搜索活动,并让搜索活动在完成setResult()使用setResult()将选定事件返回到主活动。 In this way the search activity doesn't have to start the main activity again, it just "goes back to it". 通过这种方式,搜索活动不必再次启动主活动,而只是“回到它”。

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

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