8

I have an app where after sign in it throws you at the welcome screen. I put a Toast to see when the onResume fires, but it also fires after onCreate

protected void onResume(){
    super.onResume();
    Database openHelper = new Database(this);//create new Database to take advantage of the SQLiteOpenHelper class
    myDB2 = openHelper.getReadableDatabase(); // or getWritableDatabase();
    myDB2=SQLiteDatabase.openDatabase("data/data/com.example.login2/databases/aeglea", null, SQLiteDatabase.OPEN_READONLY);//set myDB to aeglea
         cur = fetchOption("SELECT * FROM user_login");//use above to execute SQL query
         msg.setText("Username: "+cur.getString(cur.getColumnIndex("username"))
                     +"\nFull name: "+cur.getString(cur.getColumnIndex("name"))+" "+cur.getString(cur.getColumnIndex("last"))
                     +"\ne-mail: "+cur.getString(cur.getColumnIndex("email"))
                     +"\nAeglea id:"+cur.getString(cur.getColumnIndex("uid")));

         Toast.makeText(getApplicationContext(), "RESUMED", Toast.LENGTH_SHORT).show();
}

It comes from:

 //create new intent
 Intent log = new Intent(getApplicationContext(), Welcome.class);
 // Close all views before launching logged
  log.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  startActivity(log);
   // Close Login Screen
   finish();

I am baffled. Please offer some experience here

2
  • 1
    onPause or onResume?
    – zapl
    Aug 30, 2012 at 18:58
  • @zapl Sorry I will edit my Question Aug 30, 2012 at 19:00

4 Answers 4

21

Well i do not understand very well what you are trying to ask or what is the question here. BUT i will recommend you to read the "Android Activity LifeCycle" and that will clear many of your doubts cause in android is not the same as other languages or platforms.

enter image description here

Note: The OnResume is call each time the activity is "visible", so as many times as your activity becomes visible, the same number of times your method will be called. If you just want to call the method the first time, then the OnCreate is what your looking for.

0
7

Please take a look at the activity lifecycle state chart.

This is the order the methods are being called:

  1. onCreate()
  2. onStart()
  3. onResume()
  4. -> activity is running

http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle

2
  • Yes I assumed as much but I thought that the onResume() was triggered when the onPause has fired. Aug 30, 2012 at 19:17
  • 1
    It's totally fine. Some things are just different on Android, but when you know them you'll love it. In the beginning I was shocked when I got to know that onActivityResult(..) is called before onResume(..), in case you use it any soon.
    – Ivo
    Aug 30, 2012 at 19:22
2

onResume after onCreate is the normal Activity Lifecycle

The reason you get onStart and onResume called even on the first launch is that it makes writing code easier.

You can assume that before you return to onResume you will get onPause called since there is no way to exit the "resumed" state without onPause. That behavior can be used to initialize things in onResume and to de-initialize them without further checking in onPause. If you can't be sure that onResume was called at the start that whole scheme breaks.

On a sidenote: Don't access your database from any of the onXYZ methods since that will block the UI thread which should rather draw the UI and handle touch events.

0

Just to add a way around for this. Declare a variablel int intResume = 0; Then implement the following onResume:

@Override
    public void onResume()
    {
        super.onResume();
        intResume++;
        // here put codes for after onCreate & "true resume"

        if (intResume>1) {
            intResume = 10;
            // here put codes for "true resume"
        }
    }

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.