0

行 ID に基づいてデータベースから情報を取得するリスト アプリケーションに取り組んでいます。このページには、クリックすると新しいアクティビティをロードするトランザクションの追加ボタンもあります。

このアクティビティは、NavUtils クラスを使用して、ユーザーが上部のナビゲーションのホーム ボタンを押した場合にリストに戻りますが、変数が保存されていない場合に戻ります。この変数を 2 つの画面間でそのまま保持したり、NatUtils.navigateUpFromSameTask() で戻したりすることはできますか?

    public class DisplayAccountActivity extends Activity {

    private long account_id;       

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_display_account);
            // Show the Up button in the action bar.
            setupActionBar();

            // Receive the intent
            Intent intent = getIntent();
            account_id = intent.getLongExtra(MainActivity.ACCOUNT_ID, 0);
    }

    public void addTransaction (View view) {
            Intent intent = new Intent(this, AddTransactionActivity.class);
            startActivity(intent);
    }

    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                    getActionBar().setDisplayHomeAsUpEnabled(true);
            }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
                    NavUtils.navigateUpFromSameTask(this);
                    return true;
            }
            return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onResume() {
            super.onResume();
            System.out.println("On resume the ID is: "+this.account_id);
    }

    @Override
    protected void onPause() {             
            super.onPause();
            System.out.println("On pause the ID is: "+this.account_id);
    }

   }
4

2 に答える 2

0

インテントを使用して、アクティビティ間でデータを渡すことができます。

最初のアクティビティで:

Intent i= new Intent("com.example.secondActivity");
// Package name and activity
// Intent i= new Intent(MainActivity.this,SecondActivity.Class);
// Explicit intents
i.putExtra("key",mystring);
// Parameter 1 is the key
// Parameter 2 is your value
startActiivty(i);

2 番目のアクティビティでそれを取得します。

Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
//get the value based on the key
}

編集:

値を保持したい場合は、共有設定を使用してください

http://developer.android.com/guide/topics/data/data-storage.html#pref

于 2013-04-21T16:23:29.200 に答える
0

変数を静的にすることができます。その後、必要に応じて onCreate などでリセットする必要があるときはいつでもリセットできます。

于 2013-04-21T16:22:18.830 に答える