简体   繁体   English

建议在应用程序中导航时保持数据持久

[英]Advises to keep data persisted when navigating back in application

I am currently building up an application and I am facing a persistance issue. 我目前正在构建一个应用程序,并且面临持久性问题。 I have 2 activities A and B. In AI define info 'aa' and in B info 'bb' ('aa' being provided to B via intent). 我有2个活动A和B。在AI中定义信息“ aa”,在B中定义信息“ bb”(通过意图向B提供“ aa”)。 When in B and moving back to A via via the navigation back button (in action bar) I would like A to have access to 'bb' value so that I can perform some logic based on it. 在B中并通过导航后退按钮(在操作栏中)通过A返回到A时,我希望A可以访问'bb'值,以便我可以基于该值执行一些逻辑。 What would be to you the best way to persist this data to access it in A ? 对您来说,持久保存此数据以在A中访问它的最佳方法是什么? Thanks in advance for your time and help on this one. 在此先感谢您的时间和帮助。 Cheers, Benjamin 干杯本杰明

You could try using onActivityResult. 您可以尝试使用onActivityResult。

In activity B: 在活动B中:

@Override
public void onBackPressed() {
   // super.onBackPressed();

    Intent intent = new Intent();
    //put something in intent
    setResult(RESULT_OK, intent);
    finish();
}

In activity A: 在活动A中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {

            //get extra...

        }
    }
}

when you start Activity B from Activity A, use startActivityForResult(Intent, int); 从活动A启动活动B时,请使用startActivityForResult(Intent, int); instead of the normal startActivity(Intent); 而不是正常的startActivity(Intent); and override onActivityResult() in Activity A 并覆盖活动A中的onActivityResult()

eg. 例如。

public class ActivityA extends Activity{

    private static final int MY_REQUEST_CODE = 123;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);

        //Setup your activity and views...

        //Assuming that you start ActivityB when a button is clicked.
        btnExample.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){

                 Intent intent = new Intent(ActivityA.this, ActivityB.class);

                 //Setup your intent and put the data.

                 startActivityForResult(intent, MY_REQUEST_CODE);

            }
        });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent backIntent)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == RESULT_OK && requestCode == MY_REQUEST_CODE && backIntent != null){
            //Handle your logic here.
        }
    }
}

and in Activity B, Override onBackPressed 在活动B中,重写onBackPressed

public class ActivityB extends Activity{

     //Setup your activity

     @Override
     public void onBackPressed()
     {
         Intent backIntent = new Intent();
         //Put the data to backIntent
         setResult(RESULT_OK, backIntent);
         super.onBackPressed();
     }

     //The rest of your code

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM