-3

When I click the back button It goes to previous action rather than Previous Activity

what is want to do is this : 1.Activity next-> 2.Activity2 next-> 3.DialogFragment(select user from DialogFragment) next-> Activity2 (with selected user) backPressed-> Activity.

what i am at is : 1.Activity next-> 2.Activity2 next-> 3.DialogFragment(select user from DialogFragment) next-> Activity2 (with selected user) backPressed-> 5.DialogFragment backPressed->Activity

Scenario is: When i open this Activity and selecting user from dialogFragment from OnClick Event as shown in below Image:

enter image description here

after selecting user screen is:

enter image description here

when i click the back button on Top right Corner it again opens the same fragment rather than opening previous activity.. I have Tried lot of ways but Could not find solution. Please Help.

enter image description here]3

my code:

//

Back button in toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitleTextColor(getResources().getColor(R.color.o2htextOne));
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        final Drawable upArrow = getResources().getDrawable(R.drawable.back_arrow);
        upArrow.setColorFilter(getResources().getColor(R.color.o2htextOne), PorterDuff.Mode.SRC_ATOP);
        getSupportActionBar().setHomeAsUpIndicator(upArrow);
        {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            Intent intent=new Intent();



        }
 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home)
       {

            finish();

        }
      return true;

    }
Akshay
  • 111
  • 1
  • 12

2 Answers2

2
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
upArrow.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            ActivityCompat.finishAfterTransition(AgentCreateIntimation.this);
        }
});

try to use this in the place of

{
   getSupportActionBar().setDisplayHomeAsUpEnabled(true);
   Intent intent = new Intent();
}
Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
Shaifali Rajput
  • 1,279
  • 12
  • 30
-1

Just check this condition in onBackPressed and do the action what ever you want:

@Override
public void onBackPressed() {
   if (dialogObj != null && dialogObj.show()) {
      dialogObj.dismiss();   
   } else {
      //this.finish();
      super.onBackPressed();
   }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Shaik Faizal
  • 74
  • 1
  • 9