English 中文(简体)
如何将“活动”置于前台(如果不存在则创建)?
原标题:How to bring an Activity to foreground (or create if not existing)?

I am intercepting sms messages with some information in them. Then in my SmsListener I m creating notification to show in statusbar. Then, when user clicks on a notification I want

  1. Bring MainActivity to foreground (If such activity does not exist yet it should be created)
  2. Pass to it data from the sms
  3. Perform some ui changes basing on this data in this MainActivity

我的活动定义为

    <activity
        android:name=".MainActivity"
        android:screenOrientation="sensor"
        android:label="@string/app_name"
        android:launchMode="singleTask"/>

活动启动为

 Intent i = new Intent();
 i.setClass(context, MainActivity.class);
 i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 context.startActivity(i);

此外,在我的活动中,我覆盖了方法onNewActivity

 @Override
 public void onNewIntent(Intent intent){
    super.onNewIntent(intent);

    // I have data from broadcast in intent variable passed to this activity
    processDataFromBroadcast(intent);
}

如果MainActivity已经存在,但如果MainActivity不存在,它会启动,但未调用onNewIntent

Then I tried to invoke processDataFromBroadcast from onCreate: processDataFromBroadcast(getIntent()). First time data is passed correctly from my broadcast to the activity. However if MainActivity is sent to background and then again brought to foreground either onCreate or onNewIntent is called and processDataFromBroadcast is executed again with intent sent by broadcast and thus my MainActivity is updated with data from broadcast every-time the app is bringing to foreground - the latter is unwanted, how can I make my activity to forget this intent after first handling. Here is sample application.

最佳回答

对于只启动自身一个实例的活动,请查看<;活动>manifest元素,尤其是android:launchMode。您希望使用singleTasksingInstance对其进行配置。

若要将数据传递给活动,请将数据添加到用于打开活动的Intent中。若要传递带有意图的数据,请在发送数据之前使用Intent的putExtra()方法,并使用getExtra(()方法在接收活动中检索数据。

我假设您大致知道意图是如何工作的,但如果不知道,您可以通过查看这篇安卓开发者文章。

问题回答

如果你的问题仍然没有解决,因为我刚刚遇到了同样的问题,下面是我解决它的方法:

我把一个时间戳作为intentId,作为创建过程中的一个额外的intentId。第一次,我在onCreate()onNewIntent()中处理意图,我正在读取意图Id并将其存储为最后处理的意图。所以下次调用<code>onCreate()</code>或<code>on NewInteg()<-code>时,我可以检查intentId,如果它等于上次处理的intent的id,我将忽略它!它不知道这对你的情况是否有帮助,也许你可以采用它。

为了使intetId独立于活动生命周期,您可以将其持久化为用户默认值。

我同意在onNewIntent中调用setIntent(new Intent())就可以了。

现在回答已经晚了,但这可能对其他寻求解决方案的人有所帮助。

只需添加以下代码行:

Intent mIntent = new Intent(this, SplashActivity.class);
mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // You need this if starting the activity from a service
mIntent.setAction(Intent.ACTION_MAIN);
mIntent.addCategory(Intent.CATEGORY_LAUNCHER);

其中,SplashActivity是作为应用程序第一个屏幕的初始应用程序的名称。

希望能有所帮助。:)





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签