Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is onResume() called before onActivityResult()?

Here is how my app is laid out:

  1. onResume() user is prompted to login
  2. If user logs in, he can continue using the app 3. If the user logs out at any time, I want to prompt login again

How can I achieve this?

Here is my MainActivity:

@Override     protected void onResume(){         super.onResume();          isLoggedIn = prefs.getBoolean("isLoggedIn", false);          if(!isLoggedIn){             showLoginActivity();         }     } 

Here is my LoginActivity:

@Override         protected void onPostExecute(JSONObject json) {             String authorized = "200";             String unauthorized = "401";             String notfound = "404";             String status = new String();              try {                 // Get the messages array                 JSONObject response = json.getJSONObject("response");                 status = response.getString("status");                  if(status.equals(authorized)){                     Toast.makeText(getApplicationContext(), "You have been logged into the app!",Toast.LENGTH_SHORT).show();                     prefs.edit().putBoolean("isLoggedIn",true);                      setResult(RESULT_OK, getIntent());                     finish();                 }                 else if (status.equals(unauthorized)){                     Toast.makeText(getApplicationContext(), "The username and password you provided are incorrect!",Toast.LENGTH_SHORT).show();                      prefs.edit().putBoolean("isLoggedIn",true);                 }                 else if(status.equals(notfound)){                     Toast.makeText(getApplicationContext(), "Not found",Toast.LENGTH_SHORT).show();                      prefs.edit().putBoolean("isLoggedIn",true);                 }             } catch (JSONException e) {                 System.out.println(e);             } catch (NullPointerException e) {                 System.out.println(e);             }         }     } 

After the user has successfully logged in:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {         if (resultCode == RESULT_OK) {             Toast.makeText(getApplicationContext(), "BOOM SHAKA LAKA!",Toast.LENGTH_SHORT).show();         }     } 

The problem is, onResume() is called before onActivityResult() so when the user has successfully logged in, my main activity does not get notified because onResume() gets called first.

Where is the best place to prompt for login?

like image 441
Sheehan Alam Avatar asked Nov 23 '10 05:11

Sheehan Alam


People also ask

What is called after onActivityResult?

you are right, onActivityResult() is getting called before onResume().

Is onResume called after onCreate?

onResume() will never be called before onCreate() . Show activity on this post. onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() .

Is onActivityResult called before onCreate?

The Bundle passed to onCreate() in this case is the Bundle most recently used to the save the Activity instance state (from onSaveInstanceState() ). Android calls onStart() on the new Activity. Android calls onActivityResult() on the new Activity. Android calls onResume() on the new Activity.

What is onResume method?

onResume() is one of the methods called throughout the activity lifecycle. onResume() is the counterpart to onPause() which is called anytime an activity is hidden from view, e.g. if you start a new activity that hides it. onResume() is called when the activity that was hidden comes back to view on the screen.


2 Answers

The call to onActivityResult happens before onResume, actually (see the docs). Are you sure you're actually starting the activity you wanted with startActivityForResult and that you're setting the result of the invoked activity to RESULT_OK before returning a value to your activity? Try just putting a Log statement in your onActivityResult to log that value and make sure that gets hit. Also, where are you setting the value of the isLoggedIn preference? It seems like you should be setting that to true in your login activity before it returns anyways, but that's clearly not happening.

Edit

The docs say:

You will receive this call immediately before onResume() when your activity is re-starting.

like image 118
Yoni Samlan Avatar answered Sep 24 '22 05:09

Yoni Samlan


With fragments it isn't even as simple as onActivityResult() being called before the call to onResume(). If the activity that you are returning to was disposed of in the interim, you will find that calls to (for example) getActivity() from onActivityResult() will return null. However, if the activity has not been disposed of, a call to getActivity() will return the containing activity.

This inconsistency can be a source of hard-to-diagnose defects but you can check the behaviour of your application by enabling the developer option "Don't keep activities". I tend to keep this turned on - I'd rather see a NullPointerException in development than in production.

like image 32
Phil Haigh Avatar answered Sep 23 '22 05:09

Phil Haigh