1

I have started a new activity of itself by calling start activity. But after the activity get started, in the method onNewIntent the finish() is not get called!!.

WebActivity.java

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
    if (intent.getStringExtra("url") != null) {
        Intent intent1 = new Intent(getBaseContext(), WebActivity.class);
        intent1.putExtra("url",intent.getStringExtra("url"));
        startActivity(intent1);
        finish();
    }
}
Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
  • 1
    just remove setIntent(intent); because it stores current activity intent so that you come back to the same activity. check [this link](http://stackoverflow.com/questions/7748392/is-there-any-reason-not-to-call-setintent-when-overriding-onnewintent) for more information. – Mehran Zamani Feb 13 '17 at 06:13

3 Answers3

3

FYI

Debug at first and Remove below unwanted line

super.onNewIntent(intent);
setIntent(intent); 

Call finish() within Intent

Intent intent1 = new Intent(getBaseContext(), WebActivity.class);
intent1.putExtra("url",intent.getStringExtra("url"));
finish();
startActivity(intent1)

You can also try with FLAG_ACTIVITY_NO_HISTORY .

If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the noHistory attribute.

IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
3

It is because of the onNewIntent is not being called!!!

I put this on where this activity is get called.

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

then its works like a charm!!.

It is because of calling new activity. And FLAG_ACTIVITY_SINGLE_TOP makes onNewIntent get called. This FLAG_ACTIVITY_SINGLE_TOP will not start a new Activity.

Kiran Benny Joseph
  • 6,755
  • 4
  • 38
  • 57
1

I was using onNewIntent for search implementation in Android. I came across the problem that onNewIntent wasn't being called when I used the Go button on the keyboard in the emulator. I solved the issue by placing my code for handling the intent in the onCreate method also. This needs to be done to make sure that the intent action is handled when a fresh instance of the activity is started.

This posed a problem as onCreate is called whenever the activity is restored from a previous state too. So, the intent handling method gets called even when an orientation change occurs.

Solution : Use if (savedInstanceState==null) to determine if activity is being restored from a previous state, or is it a fresh search.