0

I have a problem with Shared Element Activity Transition between activities. I have MainActivity, it has a recyclerview with boxes (recyclerview.horizontal). Each box when clicked will go to the corresponding activity. The problem that appears when I click on a box, I switch to the second activity, in the second activity I press a button to switch to the third activity. And here I swipe to right to return to the MainActivity with transition and I want it to transition right to the box corresponding to the 3rd activity in the recyclerview in MainActivity. So, my purpose is:

MainActivity (Shared Element Activity Transition)-> Second Activity -> Third Activity (Shared Element Activity Transition)-> MainActivity (exactly scroll to position for Third Activity in RecyclerView).

My MainActivity I hope everyone offer me the solution. Thank you so much.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Phú Huỳnh
  • 65
  • 1
  • 7

1 Answers1

0

You can use startActivityForResult instead of startActivity in SecondActivity when you are going to start ThirdActivity.Like this :

Intent i = new Intent(this, ThirdActivity.class);
startActivityForResult(i, 1);

And when you are finishing your ThirdActivity

Intent returnIntent = new Intent();
returnIntent.putExtra("activity_finish",true);
setResult(Activity.RESULT_OK,returnIntent);
finish();

If you use startActivityForResult() then it gives callback in the Activity who started it,so as soon as ThirdActivity finishes, it will return to the onActvityResult() in SecondActivity.Where you have to check result_code and request code like this :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1) {
    if(resultCode == Activity.RESULT_OK){
        boolean isActivityFinish=data.getBooleanExtra("activity_finish");
         if(isActivityFinish){
          // finish your Second Activity here
          }


    if (resultCode == Activity.RESULT_CANCELED) {
        //Write your code if there's no result
    }
}

}

For more info : How to manage `startActivityForResult` on Android?

Ajay Chauhan
  • 1,471
  • 4
  • 17
  • 37
  • So, I want to thank you, because you spend your time to answer me. But, I think your solution has just solved a part of mine problem. I apply your solution, and I solved scroll exactly to position, but I want to have exit transition at that box. I means, at MainActivity I clicked ApartmentManagement box and transition (I use https://developer.android.com/training/transitions/start-activity) to ApartmentManagementActivity. After that, I clicked an item to go to InvoiceManagementActivity. At here, I want to finish InvoiceManagementActivity and come back to MainActivity with transition. – Phú Huỳnh Oct 30 '18 at 04:30
  • Actually that transition you didn't mention in your problem, you have to use the same exit transition in InvoiceManagementActivity that you are using in ApartmentManagementActivity so when you are finishing your InvoiceManagementActivity this will give the same feel as you are getting in ApartmentManagementActivity – Ajay Chauhan Oct 30 '18 at 05:32
  • I was trying to solve the problem which you asked in your question,if my solution solves your problem please accept and upvote . – Ajay Chauhan Oct 30 '18 at 05:34