Android创建新的进程,而不是返回到后台活动



我有一个简单的活动,用于启动和停止记录GPS数据的后台服务。然后,我通过单击home按钮将进程置于后台。

当我通过点击它的图标返回到应用程序时,它会创建一个新进程,而不是恢复到原始活动。如果我然后关闭这个进程(通过点击后退按钮),它调用onDestroy(),然后返回到我原来的活动屏幕,显示服务仍在记录。这对于应用程序的预期使用是非常不方便的。

这是我的理解,原来的活动应该从活动堆栈弹出,而不是一个新的进程正在创建。

下面是我的活动代码。我很感激任何人对此的解释。

public class MainActivity extends Activity {
    GPSTracker gps;
    private static final String TAG = "MEDIA";
    private String tv;
    private boolean record = false;
    private   TextView status = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (savedInstanceState != null) 
            record = savedInstanceState.getBoolean("Status");
        Toast.makeText(getApplicationContext(),"onCreate",Toast.LENGTH_LONG).show();
        setContentView(R.layout.activity_main);
    }
    public void StartRecord(View v) {
        Button btn = (Button) findViewById(R.id.record);
        status = (TextView) findViewById(R.id.status);
        if (record == false) {
            btn.setText("Stop Record");
            status.setText("... Recording ...");
            record = true;
            ComponentName comp = new ComponentName(getPackageName(), LogService.class.getName());
            ComponentName service = startService(new Intent().setComponent(comp));
        }
        else {
            record = false;
            status.setText("... Standing By ...");
            stopService(new Intent(MainActivity.this,LogService.class));
            btn.setText("Start Record");
        }
    }
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        Toast.makeText(getApplicationContext(), "onSaveInstanceState",Toast.LENGTH_LONG).show();
        savedInstanceState.putBoolean("Status", record);
        // etc.
    }
    @Override
    public void onResume() {
        super.onResume();  // Always call the superclass method first
        Button btn = (Button) findViewById(R.id.record);
        Toast.makeText(getApplicationContext(), "onResume",Toast.LENGTH_LONG).show();
        if (record == true) 
            btn.setText("Stop Record");
        else
            btn.setText("Start Record");
    }
    @Override
    public void onStart() {
        super.onStart();  // Always call the superclass method first
        Toast.makeText(getApplicationContext(), "onStart",Toast.LENGTH_LONG).show();
    }
    @Override
    public void onRestart() {
        super.onRestart();  // Always call the superclass method first
        Toast.makeText(getApplicationContext(), "onRestart",Toast.LENGTH_LONG).show();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();  // Always call the superclass method first
        Toast.makeText(getApplicationContext(), "onDestroy",Toast.LENGTH_LONG).show();
    }
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // Restore UI state from the savedInstanceState.
        // This bundle has also been passed to onCreate.
        Toast.makeText(getApplicationContext(), "onRestoreInstanceState",Toast.LENGTH_LONG).show();
        record = savedInstanceState.getBoolean("Status");
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

试试这个并覆盖下面的函数,它应该在你的情况下工作良好。

onResume() -> set record=true

onPause() -> set record=false

最新更新