Java – Android – Instead of starting a new intent for a new class/activity, how do I go back to opening an activity/class?

Android – Instead of starting a new intent for a new class/activity, how do I go back to opening an activity/class?… here is a solution to the problem.

Android – Instead of starting a new intent for a new class/activity, how do I go back to opening an activity/class?

My code allows me to start a new Activity/Class (class):

Intent intent = new Intent(activity1.this, activity2.class); 
startActivity(intent); 
finish(); 

What if I have an activity that is already open and just want to return to it instead of reopening a new one, opening the same activity multiple times? So I want to switch back to an already open activity/class?

Solution

Intent intent = new Intent(activity1.this, activity2.class); 
intent .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);

Related Problems and Solutions