为什么在活动开始时调用onResume()?

May*_*You 7 android onresume android-activity

我有一个应用程序,登录后它会在欢迎屏幕上引发你.我放了一个Toast来看看onResume何时触发,但它也会在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();
}
Run Code Online (Sandbox Code Playgroud)

它来自:

 //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();
Run Code Online (Sandbox Code Playgroud)

我很困惑.请在这里提供一些经验

Jor*_*lar 20

好吧,我不太清楚你想要问什么,或者这里的问题是什么.但我会建议你阅读" Android Activity LifeCycle ",这将清除你在android中的许多疑惑,因为它与其他语言或平台不同.

在此输入图像描述

注意:每次活动"可见"时都会调用OnResume,因此,当您的活动变得可见时,调用OnResume的次数与调用方法的次数相同.如果您只是想第一次调用该方法,那么OnCreate就是您所寻找的.


Ivo*_*Ivo 6

请查看活动生命周期状态图表.

这是调用方法的顺序:

  1. 的onCreate()
  2. 在onStart()
  3. 的onResume()
  4. - >活动正在运行

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