安卓活动生命周期在函数的调用之间产生混乱

问题描述 投票:0回答:1

当我运行这个应用程序时,我首先得到的是 "On Resume",而不是 "On start",甚至 "On create "也不显示,请告诉我为什么?"On Restart "toast也不显示,但测试正在更新。 public class MainActivity extends AppCompatActivity {

    public int test=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast t=Toast.makeText(getApplicationContext(),"On create",Toast.LENGTH_SHORT);
        t.setGravity(Gravity.CENTER_VERTICAL,20,20);
        t.show();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Toast t=Toast.makeText(getApplicationContext(),"On restart",Toast.LENGTH_LONG);
        t.setGravity(Gravity.CENTER_VERTICAL,20,20);
        TextView num=(TextView) findViewById(R.id.textNum);
        test++;
        num.setText(String.valueOf(test));
        t.show();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Toast.makeText(getApplicationContext(),"On start",Toast.LENGTH_SHORT).show();
    }
    @Override
    protected void onResume() {
        super.onResume();
        Toast t=Toast.makeText(getApplicationContext(),"On resume",Toast.LENGTH_SHORT);
        t.setGravity(Gravity.CENTER_VERTICAL,10,20);
        t.show();
    }

    @Override
    protected void onPause() {
        super.onPause();
       Toast t= Toast.makeText(getApplicationContext(),"On Pause",Toast.LENGTH_SHORT);
       t.setGravity(Gravity.CENTER_VERTICAL,20,20);
       t.show();
    }

    @Override
    protected void onStop() {
        super.onStop();
        Toast.makeText(getApplicationContext(),"On Stop",Toast.LENGTH_SHORT).show();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Toast.makeText(getApplicationContext(),"On destroy",Toast.LENGTH_SHORT).show();
    }
}
android android-lifecycle toast
1个回答
1
投票

使用 Toast 作为调试方法是非常糟糕的想法。使用日志记录,看看你的logcat。你会看到,方法被调用的顺序,他们应该。只是不要这样做。

© www.soinside.com 2019 - 2024. All rights reserved.