开发者

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

My activity is defined as

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

Activity is launched as

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

Also in my activity I have overridden method onNewActivity

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

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

It works fine if t开发者_开发问答he MainActivity already exists but if MainActivity does not exist it is started however onNewIntent was not called

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.


For an activity to launch only one instance of itself, have a look at the <activity> manifest element, and particularly android:launchMode. You want to configure it with either singleTask or singleInstance.

To pass data to your activity, you add data to the Intent you use to open it. To pass data with the intent, use the putExtra() methods of the intent before sending it off, and getExtra() methods to retrieve them in your receiving activity.

I'm assuming that you know roughly how intents work, but if not you could learn more about intents by taking a look at this Android developers article.


in case your problem is still unresolved, as I was just running into the same issue, here's how I solved it:

I am putting a timestamp as intentId as an extra upon the intent during it's creation. the first time, I am handling the intent in onCreate() or onNewIntent() I am reading the intentId and store it as the last intent handled. so the next time onCreate() or onNewIntet() is invoked I can check the intentId and if it equals the id of the last intent handled, I ignore it! It don't know if this helps in your case, maybe you can adopt it.

To keep intentId independent from activity lifecycles you could persist it in the userdefaults.

I agree that one would expect calling setIntent(new Intent()) in onNewIntent should do the trick.


It it late to answer, but it might be helpful to others looking for the solution.

Just add below lines of code :

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);

Where SplashActivity is the name of initial application that is the first screen of your application.

Hope it helps. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜