1

I am trying to make an application. In my app there is a recycler that contains images, when you click on particualr item it goes to middle activity (that activity gets link to load image(by Picasso) and shows the image), then you can go to the last activity that "does something". But there is a problem in my back arrow thing: enter image description here The Last activity is a child of Middle activity, so when you click on the arrow icon it goes to the Middle Activity. BUT Middle activity is supposed to get data of image (like link to load image from recycler item). So when the arrow on LastActivity is clicked it goes to MiddleActivity but doesn't work properly because doesn't have the essential link of image.

It is my click ItemClickListener in my Adapter for recycler:

//IMPLEMENT CLICK LISTENER
        holder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(View v, int pos) {
                String getTitle = mMainList.get(pos).getKeyId();
                Toast.makeText(v.getContext(),getTitle,Toast.LENGTH_SHORT).show();
                //Go to the next Activity
                Intent show_more = new Intent(v.getContext(), MiddleActivity.class);
                show_more.putExtra("image_link", mMainList.get(pos).getImageLink());
                v.getContext().startActivity(show_more);
            }
        });

It is intent in MiddleActivity that goes to LastActivity

Intent go_to_last = new Intent(MiddleActivity.this, LastActivity.class);
                        String image_link_push = getIntent().getStringExtra("image_link");
                        go_to_last.putExtra("image_link_push", image_link_push);//push link further
                        startActivity(go_to_last);

My thought is that I need to customize the back arrow and send by intent the image link. Procces would be looking like this:

MainAct (send image link by intent) --- MiddleAct (get the link, and send to the next) --- LastAct (get the link,on pressed back arrow send the same link to Middle) link would be dragged through all the activities

I tried to make it by onBackPressed method (in LastActivity) but it does nothing:

image_link_back = getIntent().getStringExtra("image_link_push");

 @Override //!!
    public void onBackPressed()
    {
        Intent intent = new Intent(LastActivity.this, MiddleActivity.class);
        intent.putExtra("image_link_back", image_link_back);
        startActivity(intent);
        super.onBackPressed();
        finish();
    }

Hopefully I have explained my problem correctly.

How can I "customize" the arrow back and solve the problem? I would appreciate you answering me.

Amazonian pet
  • 47
  • 1
  • 1
  • 6
  • 2
    why not use StartActivityForResult for your middle activity? then just setResult(link) in your last activity https://stackoverflow.com/questions/14785806/android-how-to-make-an-activity-return-results-to-the-activity-which-calls-it – Angel Koh Dec 03 '19 at 15:56
  • Does this answer your question? [How to manage startActivityForResult on Android?](https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android) – patrick.elmquist Dec 03 '19 at 19:58

1 Answers1

2

Because when user click arrow back the activity manger closed current activity and launch parent activity so onCreate will be called again

Solution : when user click arrow back finish current Activity (LastActivity) without start parentActivity(MiddleActivity) again by insert this code in LastActivity

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            //listen to back arrow in app bar
            finish();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}
Mahmoud Abu Alheja
  • 3,343
  • 2
  • 24
  • 41