How do I avoid onCreate() being called when starting an Activity?

后端 未结 5 364
难免孤独
难免孤独 2021-01-13 14:27

I want to reload an activity from stack.

I use startActivity() to start new activities. When I\'m on Activity D I want to reload Activity A

相关标签:
5条回答
  • 2021-01-13 14:46

    there is tag called launchMode for activity in the manifest. checkout this link. and this will not call onCreate but it will call onNewIntent, where you can reinitialize you stuff.

    0 讨论(0)
  • 2021-01-13 14:54

    The following is not true. startActivityForResult() and startActivity() only differ in the return target of the called Activity

    try using startActivityForResult() rather than startActivity(). I believe this does not completely end the activity and start it again. I would recommend using this link in order to further read on how to implement such a method.

    So point 2 of @Kgrover does not hold too.

    The Intent flag http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_REORDER_TO_FRONT does exactly this.

    Keep an eye out on the Intent flags whenever you have requirements centered around Activity transitions. The system has excellent support.

    This is equivalent to Sam Quest's solution, the only difference being that if you set the launchMode, the stack-behavior of your Activity is hardcoded i.e. your Activity A is always in the singleTask mode.

    0 讨论(0)
  • 2021-01-13 14:56

    You have to take a totally different approach. It doesn't matter if you start your Activity with startActivity() or startActivityForResult() because onCreate(), onStart() and onResume() will be called when you start an Activity.

    Now if you have a method in your Activity class that starts another thread to do some work then you have to work with flags. If your Activity requires to automatically start the thread on first execution then you have to wrap it around an if clause to check for a flag you set when it is first run.

    The idea is to have your Activity set a boolean to true in either your Application instance or SharedPreferences when the thread is first executed. When you come back to that Activity and don't want that thread to be run automatically due to onCreate() being called then you have to wrap your calling code around some if clause like in the example below.

    Here is an example.

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        // Other stuff
    
        if (!YourApplicationInstance.wasCalled) {
            // Run your thread or do something else you want to do only once.
    
            // Set the wasCalled flag to true to not run this code again
            // if onCreate() is called a second time.
            YourApplicationInstance.wasCalled = true;
        }
    }
    

    You'll have to read Using Application context everywhere? to understand how to implement my pseudo class YourApplicationInstance.

    0 讨论(0)
  • 2021-01-13 15:06
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                return true; 
        }
        return super.onOptionsItemSelected(item);
    }
    

    This will kill the child activity. So parent activity is not recreated

    0 讨论(0)
  • 2021-01-13 15:07

    1) Although I am not sure, you could try using startActivityForResult() rather than startActivity(). I believe this does not completely end the activity and start it again. I would recommend using this link in order to further read on how to implement such a method.

    2) Alternatively, when you go from activity D -> A, continue to use startActivity(), but pass in a dummy extra. Then in activity A, use an if statement:

     if(!(this.getIntent().hasExtra("dummyStringExtra")) {
      //fire the data thread here 
    }
    

    Cheers. I hope this helps.

    0 讨论(0)
提交回复
热议问题