onNewIntent not get called
Asked Answered
B

3

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();
    }
}
Burdock answered 13/2, 2017 at 6:5 Comment(1)
just remove setIntent(intent); because it stores current activity intent so that you come back to the same activity. check this link for more information.Skippy
B
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.

Burdock answered 13/2, 2017 at 6:18 Comment(1)
glad to hear . did you try with FLAG_ACTIVITY_NO_HISTORY ?Dense
D
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.

Dense answered 13/2, 2017 at 6:8 Comment(3)
@CoDfather Refer the official documents to understand the activity lifecycle:developer.android.com/reference/android/app/Activity.htmlAlerion
if the finish() is called first, how it goes to the startActivity(intent1) ??Burdock
thats why i suggest FLAG_ACTIVITY_NO_HISTORY Dense
B
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.

Burdock answered 13/2, 2017 at 6:18 Comment(1)
glad to hear . did you try with FLAG_ACTIVITY_NO_HISTORY ?Dense
S
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.

Sullen answered 2/6, 2017 at 12:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.