本文介绍了活动周期 - startActivity()与演员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有两个独立的应用程序,无论是单独执行任务,但对当我需要一个应用程序如果有使用其他有限场合倍。

I am currently have two seperate applications, both perform seperate tasks, but there is on limited occasion times when I need one application to use the other if its there.

于是我就用一个函数来检查所需的应用程序存在:

So I use a function to check the required application exists:

public static boolean isIntentAvailable(Context context, String action)
{
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

如果确实如此,那么我用下面的启动活动有一个额外的有:

If it does, then I use the following to start the activity with an extra on there:

if (isIntentAvailable(ListPOI.this, "com.me.myapp.MY_MAP"))
{
    Intent i = new Intent("com.me.myapp.MY_MAP");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.putExtra("place", true);
    startActivity(i);
}

该setFlags意味着如果用户presses回家,他们回到了第一个应用程序,它会打开,它不会打开这里叫做第二个应用程序。

The setFlags means if the user presses home, and they go back to the first app, it opens, it doesn't open the second app called here.

这一切工作正常,还是第一次。调用这个第二次以后然而,第二个应用程序恢复,因此它不会拿起已通过了额外的,我怎么能保证我得到额外的?

This all works fine, the first time. However after calling the this the second time, the second app resumes, so it doesn't pick up the 'extra' which has been passed, how can I ensure I get the extra?

推荐答案

最后回答我的问题已经被使用下列内容:

The final answer to my question has been using the following:

我创建了一个函数来处理加载额外的意图:

I created a function to handle loading intent extras:

private void handleIntent(Intent intent)
{
    Bundle extras = intent.getExtras();
    if (extras != null)
    {
        place = extras.getString("place");
    }
}

在我的onCreate使用:

In onCreate I use:

handleIntent(getIntent());

然后添加下面的函数来获取意图当一个活动的现有实例传递一个新的意图:

Then added the following function to get the intent when an existing instance of an activity is passed a new intent:

public void onNewIntent(Intent intent)
{
    setIntent(intent);
    handleIntent(intent);
}

我还添加了的android:launchMode =singleInstance来活动的声明与上述code中的活动清单

I also added android:launchMode="singleInstance" to the activity declaration in the manifest of the activity with the above code.

最后,从第一个包,我现在可以使用下面的code与额外启动第二包。当第二包启动后,用户可以点击第一个包(从主发射器),并得到他们所期望的应用程序,如果他们点击开始第二个包中,已经运行实例显示,但抓住了新的额外的已经过去了:

Finally from the first package, I can now use the following code to start the second package with an extra. When the second package starts, the user can click on the first package (from the home launcher) and get the app they expect, and if they click to start the second package, the 'already running' instance is shown, but captures the new extra which has been passed:

Intent i = new Intent("com.me.myapp.MY_MAP");
i.putExtra("place", place);
startActivity(i);

希望这将是有益的人 - 它可能没有做到这一点的最好办法,但它为我的作品

Hopefully this will be helpfull to someone - its probably not the best way to do this, but it works for me.

这篇关于活动周期 - startActivity()与演员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:22