1

I'm calling recreate in onActivityResult of MainActivity when certain changes are made in the app settings. After recreation, onResume is not called.

I'm also getting the error:

E/ActivityThread: Performing pause of activity that is not resumed

From this question, I understood that this function can't be called from onResume. But I'm calling them from onActivityResult. Also using handler to call recreate resolves the problem, but causes a blink that looks bad to the user. What could be the possibly wrong here? How can I use recreate without a Handler?

Any ideas will be appreciated. Thanks!

Community
  • 1
  • 1

2 Answers2

3

OnActivityResult() is called before onResume(). What you can do is set a flag in the OnActivityResult() that you can check in the onResume() and if the flag is true you can recreate the activity.

What you could actually do is to finish the activity and start the same one, isntead of recreating it. You will get the same effect. it could be something like this:

public class MainActivity extends AppCompatActivity   {

private boolean shouldRecreate = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("AG", "onCreate() called");
    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivityForResult(intent, 0);
}

@Override
protected void onResume() {
    super.onResume();
    if (shouldRecreate){
        finish();
        startActivity(new Intent(this, MainActivity.class));
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 0){
        shouldRecreate = true;
    }
}
}
Aksiom
  • 1,565
  • 4
  • 25
  • 39
  • What kind of flag should I check? Could you give an example? –  Feb 22 '16 at 21:19
  • make your own flag, something like shouldRecreate (bool). In the OnActivityResult() you set the shouldRecreate to true if the condition is the one that you want. Then in the onResume you check shouldRecreate == true, if yes, you call the recreate method. – Aksiom Feb 22 '16 at 21:21
  • I set the flag in onActivityResult and checked it in onResume. The error is still coming. –  Feb 22 '16 at 21:26
  • 1
    Make sure set false to shouldRecreate if its true. So that whenevr onresume called .it will not be called again. – SreeAndroidDev Feb 22 '16 at 21:28
  • Ok, but the it should not show the error for the first time atleast. But it is still showing. –  Feb 22 '16 at 21:32
  • I set shouldRecreate false after first time. Error still coming. –  Feb 22 '16 at 21:33
  • Use the handler to make a delay (As in the answer you gave in your question), it seems that this is the only solution for that so far, the super.onResume needs a bit of time to finish and that is why you get this error when you try to recreate the activity imidietly.. But you can still use the way with the flag to see if you need to recreate the activity. Check edit for alternate solution! – Aksiom Feb 22 '16 at 21:46
  • Yes. This takes me to base zero. Your method should be working. I don't know why the error still comes. –  Feb 22 '16 at 21:50
  • I've tried finishing and starting. Gives the same error. –  Feb 22 '16 at 22:04
  • Thanks for helping anyway! –  Feb 22 '16 at 22:08
0

I finally solved the problem by sending a broadcast from the SettingsActivity to the BroadcastReciever in the MainActivity and calling recreate() inside onRecieve().