0

What I am trying to do is, there is no chance the two activities are running at the same time. So I am using for this method in my adaptor class.

Intent intent = new Intent().setClass(v.getContext(), WinnerDetailActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        v.getContext().startActivity(intent);
((Activity)context).finish();

But when I click the back button, it doesn't go to the back activity.

What am I doing wrong?

Lucifer
  • 29,392
  • 25
  • 90
  • 143
podgradle
  • 336
  • 1
  • 10
  • 17

9 Answers9

2

Basically you should remove finish() method from your code so that it will not destroy that activity and keep it in stack.

Once you call the finish() you can not go back to previous activity. This question explains in details, what happens when you call finish() method.

Vikasdeep Singh
  • 20,983
  • 15
  • 78
  • 104
1

Remove ((Activity)context).finish(); in your code,

because here you are finishing your activity which means when you press back you don't have any activity on your stack to go back to.

Finish() method will destroy the current activity. You can use this method in cases when you dont want this activity to load again and again when the user presses back button. Basically it clears the activity from the current stack. So,no need to use finish() here

komal akhani
  • 553
  • 6
  • 20
0

Just remove this line:

((Activity)context).finish();
Cao Minh Vu
  • 1,900
  • 1
  • 16
  • 21
0

Called the Finish method of activity at button click.

finish() just lets the system know that the programmer wants the current Activity to be finished.

((Activity)context).finish();

Nitin Sharma
  • 100
  • 9
0

That is because you are finishing your activity which means when you press back you don't have any activity on your stack to go back to.

so just remove finish so that i will push WinnerDetailActivity on top of your current activity and on back of WinnerDetailActivity it will open your current activity.

Intent intent = new Intent().setClass(v.getContext(), WinnerDetailActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                        v.getContext().startActivity(intent);

And also read about FLAG_ACTIVITY_SINGLE_TOP

If set, the activity will not be launched if it is already running at the top of the history stack.

Niki
  • 1,566
  • 1
  • 19
  • 36
0
//Here intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); will alway remain in intent stack unless you finish it and once this activity is resume.. then it will act as the stack has only on Activity running...
//So just remove ((Activity)context).finish();
Intent intent = new Intent().setClass(v.getContext(), WinnerDetailActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    v.getContext().startActivity(intent);
((Activity)context).finish();
Rohan Shukla
  • 533
  • 1
  • 4
  • 16
0
There is two way to resolve this

1.Just remove ((Activity)context).finish(); because Finish() method will destroy the current activity.

2.Just Add below method in your class

 public void onBackPressed() {
        super.onBackPressed();
        startActivity(new Intent(CurrentActivity.this,DestinationActivity.class));

    }
DestinationActivity means Activity where you want to move.
Manish
  • 9
  • 1
  • 4
0

This can be done in two ways: Method 1: Whenever you're stating a New Activity from an Activity, Make sure that you don't call finish(). Calling finish() will destroy the Previous Activity in the Stack. Next Method is by Overriding onBackPressed(), by doing so you can navigate to the desired Activity.

@Override
public void onBackPressed() { 
super.onBackPressed();
    Intent intent = new Intent( thisActivity.this, yourFirstActivity.class);
    startActivity(intent);
    finish();
    }
0

OnBack Pressed Method is used to go back to previous activity, super.onBackPressed() is used to navigate to previous activity without super.onBackpress it is not possible to traverse to previous activity.

  @Override
    public void onBackPressed() 
    { 
    super.onBackPressed();
    }
Deep Adhia
  • 382
  • 4
  • 11