startActivityForResult不能正常工作与launchMode singleInstance不能正常、工作、startActivityForResult、launchMode

2023-09-12 04:21:57 作者:卸尽戎马伴你余生

我想在我的应用程序的活动堆栈活动只能有一个实例。我有几个屏幕这是ListActivities,我想没有经历的痛苦和更新列表中的ListActivity的previous实例的痛苦时ListActivity的另一个实例是改变(增加,编辑,移除等)(或者是有一个简单的方法来做到这一点?)。

注:我读过singleTop将做到这一点(虽然它破坏活动,如果你打的后退按钮),但它不工作。我有一个菜单,如果我去我的收件箱界面,然后我去快选屏幕,然后我去我的收件箱界面再次,它会创建一个新的收件箱中的活动。

现在,在我的ListActivities,我有launchMode设置为singleInstance。现在的问题是:如果我启动使用startActivityForResult的onActivityResult处理火灾的时候了(在创建新的活动之前)的另一个活动。当我执行下一屏幕上的必要行动,返回结果,onActivityResult处理程序不火。

这是怎么回事?

下面是我的火了新的活动:

 意图int​​entLaunchQuickList =新的意图(ActivityMyList.this,ActivityQuickList.class);
startActivityForResult(intentLaunchQuickList,REQUEST_QUICKLIST);
 

下面是我如何返回结果:

  @覆盖
保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
    super.onListItemClick(L,V,位置ID);
    QuickListItem qlItem = m_Adapter.getItem(位置);
    如果(qlItem = NULL和放大器;!&安培;!qlI​​tem.getQLId()=  -  1){
        意图数据=新的意图();
        data.putExtra(ql_id,qlItem.getQLId());
        如果(的getParent()== NULL){
            的setResult(Activity.RESULT_OK,数据);
        }
        其他 {
            的getParent()的setResult(Activity.RESULT_OK,数据)。
        }
    }
    完();
}
 
苹果Siri无法正常工作怎么办

下面是我的onActivityResult处理程序:

  @覆盖
公共无效onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == REQUEST_QUICKLIST){
        如果(结果code == Activity.RESULT_OK){
            捆绑额外= data.getExtras();
            如果(临时演员!= NULL){
                INT的id = extras.getInt(ql_id);
                如果(标识大于0){
                    launchQLItemsThread(ID);
                }
            }
        }
    }
}
 

解决方案

startActivityForResult 的说明文件:举个例子,如果你正在开展活动使用的singleTask启动模式,它不会在你的任务运行,因此你会立即收到取消的结果。 singleInstance 的活动都以同样的方式。

在换句话说,如果你想使用SAFR,您将需要处理多个活动实例。我会建议为存储列表状态的 ListActivity 事例的onPause 一些应用全局点(一个单或其他),并装载从那里 onResume 。这样,即使多个 ListActivity 情况下,将获得创建,最上面的一个会随时更新的数据上了年纪的人得到恢复之前,和列表将始终显示给用户的电流。

请注意,你应该这样做,无论如何,如果你的数据,就是要持续的,因为你的整个过程可以通过系统中的的onPause 通话后的任何时候被杀死,如果你还没有返回时保存的任何改变的地方,否则,容易在某些获得默默地失去了 - 经常罕见和未predictable - 的情况。在这种情况下要使用本地文件或SQLite数据库,没有坚持到网络。 的onPause 需要快速返回,因为用户可以通过该系统在其他时间没有交互,而它的运行,所以保存到本地存储,然后同步到网络,可能通过通过的onPause 推出的一项服务。

I'd like Activities on my application's Activity stack to only have one instance. I have several screens which are ListActivities and I'd like to not go through the pain and suffering of updating the lists in a previous instance of the ListActivity when another instance of that ListActivity is changed (added to, edited, removed from, etc) (or is there an easy way to do this?).

Note: I've read that singleTop will accomplish this (though it destroys the Activity if you hit the back button), but it does not work. I have a menu and if I go to my Inbox screen, then I go to my QuickList screen, and then I go to my Inbox screen again, it creates a new Inbox Activity.

Right now, on my ListActivities, I have launchMode set to singleInstance. The problem is: If I launch another Activity using startActivityForResult, the onActivityResult handler fires right away (before the new Activity is created). When I perform the necessary action on the next screen to return the result, the onActivityResult handler does not fire.

What is going on?

Here is how I fire the new Activity:

Intent intentLaunchQuickList = new Intent(ActivityMyList.this, ActivityQuickList.class);
startActivityForResult(intentLaunchQuickList, REQUEST_QUICKLIST);

Here is how I return the result:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    QuickListItem qlItem = m_Adapter.getItem(position);
    if (qlItem != null && qlItem.getQLId() != -1) {
        Intent data = new Intent();
        data.putExtra("ql_id", qlItem.getQLId());
        if (getParent() == null) {
            setResult(Activity.RESULT_OK, data);
        }
        else {
            getParent().setResult(Activity.RESULT_OK, data);
        }
    }
    finish();
}

Here is my onActivityResult handler:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_QUICKLIST) {
        if (resultCode == Activity.RESULT_OK) {
            Bundle extras = data.getExtras();
            if (extras != null) {
                int id = extras.getInt("ql_id");
                if (id > 0) {
                    launchQLItemsThread(id);
                }
            }
        }
    }
}

解决方案

From the documentation of startActivityForResult: "For example, if the activity you are launching uses the singleTask launch mode, it will not run in your task and thus you will immediately receive a cancel result." singleInstance activities are the same way.

In other words, if you want to use sAFR, you will need to handle multiple activity instances. What I would advise is storing the list state for your ListActivity instances in onPause to some app-global spot (a singleton or whatever), and loading from there in onResume. Then, even if multiple ListActivity instances will get created, the top one will always update the data before the older ones get resumed, and the lists will always appear current to the user.

Note that you should be doing that anyway if your data is meant to be persistent, because your whole process can be killed by the system any time after an onPause call, and if you haven't saved any changes somewhere by the time that returns, they are liable to get silently lost under some -- often rare and unpredictable -- circumstances. In this case you want to be using local files or SQLite databases, not persisting to the network. onPause needs to return quickly because the user can't interact with the system while it's running, so save to local storage and then sync to the network at some other time, perhaps via a service launched by onPause.

 
精彩推荐
图片推荐