Juhil Desai
Juhil Desai

Reputation: 1

Closing current and previous activity on click of button(not the back button) on current activity

I start in main activity A. From A I go to activity B and from B I go to C. Now in activity C if a button is clicked (not back button) ,activity B and C need to close and a new instance of activity B needs to be created. But if that particular button is not pressed and back button is pressed then activity C is closed and we return to previous activity B. I am making a reminder app. Activity B is a recycler view showcasing all the tasks and Activity c is to create new task. So if on activity C submit button is pressed I want activity C and B to be closed and a new instance of b to be opened i.e. recycler view with new tasks but if submit is not pressed I want to close activity C and return to original instance of B. How to do so?

Upvotes: 0

Views: 146

Answers (3)

hardkgosai
hardkgosai

Reputation: 309

Maybe you can use this 2 methods

(1) Use IntentReceiver

From second activity

  Intent i = new Intent(SecondActivity.this, ThirdActivity.class);
    i.putExtra("SecondActivity", new ResultReceiver(null) {
        @Override
        protected void onReceiveResult(int resultCode, Bundle resultData) {
            SecondActivity.this.finish();
        }
    });
    startActivityForResult(i, 1);

In third activity

   ((ResultReceiver) Objects.requireNonNull(getIntent().getParcelableExtra("MainActivity"))).send(1, new Bundle());

    //And after this finish current activity (Second activty)

    startActivity(new Intent(ThirdActivity.this, FirstActivity.class));
    finish();

(2) If Clear all previous and current activity

Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Upvotes: 1

jana
jana

Reputation: 266

Start Activity B with the intent flag FLAG_ACTIVITY_CLEAR_TOP

https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP

Upvotes: 0

Simran Sharma
Simran Sharma

Reputation: 892

You can use the method finishAffinity() to exit an activity. I hope this at least makes the problem a bit easier.

Upvotes: 0

Related Questions