Switching between activities in android?

前端 未结 7 555
有刺的猬
有刺的猬 2020-12-18 10:22

In my android application, I have following requirement.

Activity A --> Activity B(Go to A Option) --> Activity C(Go To A Option,Go To B Option)

<
相关标签:
7条回答
  • 2020-12-18 10:53

    I assume that you don't want to use Intent because whenever you use Intent for moving to activity A pressing Back key will move to the previous activity (activity C). In this case I would suggest you to include FLAG_ACTIVITY_CLEAR_TOP flag. It will destroy all the previous activity and let you to move to Activity A.

     Intent a = new Intent(this,A.class);
     a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     startActivity(a);
    

    Alternatively, you can try the FLAG_ACTIVITY_REORDER_TO_FRONT flag instead, which will move to activity A without clearing any activity.

    For more, check this.

    0 讨论(0)
  • 2020-12-18 10:58

    You can do this as:

    On back press option:

    1. onBackPressed(){ @Override public void onBackPressed() {

      // TODO Auto-generated method stub
          super.onBackPressed();
      //Your method intent
          Intent intent = new Intent(A.this, C.class);
          startActivity(intent);
      

      }

    Using a button:

    1. button.setOnClickListener( new View.OnClickListener() {

           public void onClick(View v)
      
           {
      
                   Intent intent = new Intent(A.this, C.class);
               startActivity(intent);
            // finish(); // may use finish if do not want endless loop
      }});
      
    0 讨论(0)
  • 2020-12-18 11:01

    Why can't you use,

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //Save your current instance
    }
    

    "...This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state. For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state of the user interface can be restored via onCreate(Bundle) or onRestoreInstanceState(Bundle)..."

    I think that is what you wanted. With this you can use Intent from Activity C. Yes then it goes to onCreate method, BUT there you are checking whether there is any saved instance. If there are any saved instance you no need to load the rest of the onCreate items. It will speed your application.

    0 讨论(0)
  • 2020-12-18 11:04

    If you want to re-create your Activity then you can use FLAG_ACTIVITY_SINGLE_TOP

    Intent intent = new Intent(this, A.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
    

    By doing this your Activity A will not be re-created rather you will have to override onNewIntent() which will be called in your Activity A.

    UPDATE:

    This was using Intent but as your requirement was without using Intent, so in that case better approach would be using startActivityForResult as already there are couple of answers above I am not elaborating it.

    0 讨论(0)
  • 2020-12-18 11:08

    There are two ways you can achieve this: By Starting Intent with applying Intent Filter Clear top stack, but according to your question you are not interested in this method. Second method is by Starting activities by StartActivityForResult.

    By using intent flags:

    Intent intent = new Intent(this, A.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    

    Second Way, by Using startActivityForResult:

    In Activity B:

      Intent intent=new Intent(B.this, C.class);
      intent.startActivityForResult(intent);
    

    onActivityResult method:

     protected void onActivityResult(int reqCode, int resultCode, Intent data) {
         if (resultCode == Activity.RESULT_OK ) {
             String res = data.getExtras().getString("result");
             if (res.equals("A")) {
                 String msg = "RESULT: " + res;
                 Toast.makeText(Login2.this, msg, Toast.LENGTH_SHORT).show();
                 finish();
             }
         }
     }
    

    In Activity C

     Intent intent = new Intent();
     intent.putString("result", "Hello, World");
     setResult(RESULT_OK, intent);
    
    0 讨论(0)
  • 2020-12-18 11:12

    you can passon the context of Activity B to Activity C and then in Activity C call

    ((Activity) bContext).finish();
    

    that'll close Activity B and then you can call

    ((Activity)getContext()).finish();
    

    that'll cause Activity C to close and go back to Activity A as Activity B is already closed.

    0 讨论(0)
提交回复
热议问题