Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 为什么在活动启动时调用onResume()?_Android_Android Activity_Onresume - Fatal编程技术网

Android 为什么在活动启动时调用onResume()?

Android 为什么在活动启动时调用onResume()?,android,android-activity,onresume,Android,Android Activity,Onresume,我有一个应用程序,在登录后,它会把你扔到欢迎屏幕上。我举杯庆祝onResume何时启动,但onCreate之后也会启动 protected void onResume(){ super.onResume(); Database openHelper = new Database(this);//create new Database to take advantage of the SQLiteOpenHelper class myDB2 = openHelper.get

我有一个应用程序,在登录后,它会把你扔到欢迎屏幕上。我举杯庆祝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();
}
它来自:

 //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();

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

好吧,我不太明白你想问什么或者这里的问题是什么。但我建议您阅读“”这将消除您的许多疑虑,因为android与其他语言或平台不同


注意:每次活动“可见”时都会调用OnResume,因此只要活动可见,就会调用与方法相同的次数。如果您只想在第一次调用该方法,那么OnCreate就是您想要的。

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

这是调用方法的顺序:

  • onCreate()
  • onStart()
  • onResume()
  • ->活动正在运行

  • onResume
    之后的
    onCreate
    是正常的

    即使在第一次启动时,也会调用
    onStart
    onResume
    ,这是因为它使编写代码更容易

    您可以假设,在返回到
    onResume
    之前,将调用
    onPause
    ,因为没有
    onPause
    ,无法退出“已恢复”状态。该行为可用于初始化
    onResume
    中的内容,并在不进一步签入
    onPause
    的情况下取消初始化它们。如果不能确定在开始时调用了
    onResume
    ,则整个方案都会中断


    旁注:不要从任何
    onXYZ
    方法访问您的数据库,因为这会阻止UI线程,而UI线程应该绘制UI并处理触摸事件。

    onPause
    onResume
    ?@zapl抱歉,我会编辑我的问题是的,我也这么认为,但我认为onResume()在onPause启动时被触发。完全正常。Android上有些东西是不同的,但当你了解它们时,你会喜欢它的。一开始,当我知道onActivityResult(..)在onResume(..)之前调用时,我感到震惊,以防您很快使用它。