0

GPS データを記録するバックグラウンド サービスを開始および停止する単純なアクティビティがあります。次に、ホーム ボタンをクリックしてプロセスをバックグラウンドに配置します。

アイコンをクリックしてアプリに戻ると、元のアクティビティに戻るのではなく、新しいプロセスが作成されます。このプロセスを (戻るボタンをクリックして) 閉じると、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;
    }
}
4

1 に答える 1