[android] onNewIntent() lifecycle and registered listeners

I'm using a singleTop Activity to receive intents from a search-dialog via onNewIntent().

What I noticed is that onPause() is called before onNewIntent(), and then afterwards it calls onResume(). Visually:

  • search dialog initiated
  • search intent fired to activity
  • onPause()
  • onNewIntent()
  • onResume()

The problem is that I have listeners registered in onResume() that get removed in onPause(), but they are needed inside of the onNewIntent() call. Is there a standard way to make those listeners available?

This question is related to android android-intent lifecycle

The answer is


Note: Calling a lifecycle method from another one is not a good practice. In below example I tried to achieve that your onNewIntent will be always called irrespective of your Activity type.

OnNewIntent() always get called for singleTop/Task activities except for the first time when activity is created. At that time onCreate is called providing to solution for few queries asked on this thread.

You can invoke onNewIntent always by putting it into onCreate method like

@Override
public void onCreate(Bundle savedState){
    super.onCreate(savedState);
    onNewIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
  super.onNewIntent(intent);
  //code
}

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to android-intent

Kotlin Android start new Activity Open Facebook Page in Facebook App (if installed) on Android Android - Adding at least one Activity with an ACTION-VIEW intent-filter after Updating SDK version 23 Not an enclosing class error Android Studio Parcelable encountered IOException writing serializable object getactivity() Sending intent to BroadcastReceiver from adb How to pass ArrayList<CustomeObject> from one activity to another? Android Intent Cannot resolve constructor Android Gallery on Android 4.4 (KitKat) returns different URI for Intent.ACTION_GET_CONTENT Android - java.lang.SecurityException: Permission Denial: starting Intent

Examples related to lifecycle

How to use lifecycle method getDerivedStateFromProps as opposed to componentWillReceiveProps onNewIntent() lifecycle and registered listeners Android activity life cycle - what are all these methods for? Looking to understand the iOS UIViewController lifecycle How to retrieve the dimensions of a view?