Android: Previous Activity is not finished in using FLAG ACTIVITY CLEAR TOP - java

I'm new to Android programming so I might miss something important.
What I want to do is to finish the current activity (ActivityB) and the previous activity (ActivityA).
And the users can switch between ActivityA and ActivityB using FLAG_ACTIVITY_REORDER_TO_FRONT. Now I want to finish ActivityB and the previous ActivityA and start new ActivityA.
However, seems like the previous ActivityA is still running even after finishing ActivityB.
Code is like this.
ActivityA
private void startActivityB() {
Intent intent =
new Intent(ActivityA.this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
ActivityB
private void exitActivityB() {
Intent intent =
new Intent(ActivityB.this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
#Override
private void onBackPressed() {
// finish ActivityB and the previous ActivityA
Intent intent =
new Intent(ActivityB.this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
What am I wrong with this? How can I finish the previous ActivityA and start new ActivityA?

Try following way
Intent i = new Intent(ActivityB.this, ActivityA.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);

Try this one:
#Override
private void onBackPressed() {
// finish ActivityB and the previous ActivityA
Intent intent =
new Intent(ActivityB.this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}

Try this code
Intent i = new Intent(ActivityB.this, ActivityA.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();

Related

How to return ArrayList from intent

I have code that passes an arraylist to another intent, how can I return the value of updated arraylist back to the main intent?
Arraylist in main intent code:
ArrayList<Users> vuser = new ArrayList<>();
Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
intent.putParcelableArrayListExtra("vuser", vuser);
startActivity(intent);
Do I have to pass the arraylist again to the main intent? Like this:
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
intent.putParcelableArrayListExtra("vuser", vuser);
startActivity(intent);
You can pass it using the getIntent() method. When you go back, you can override the onBackPressed() method and add your own login like the one given below. Check the code out:
#Override
public void onBackPressed(){
getIntent().putParcelableArrayListExtra("vuser", vuser);
finish();
}
This will send data to the previous activity and then finish the current activity.

Android Studio: How to send, and recieve a Class in diferent Activities

Im making a quiz game with questions on diferent topics
For Example i have activities for these topics: Flags, Capitals, Population, Economy, Continent, etc.
And i have one single ResultActivity to obtain the Score of the quiz.
The ResultActivity has a PLAY AGAIN button.
On the FlagsActivity i have this code:
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", score);
intent.putExtra("NAME_ACTIVITY", "FlagsActivity");
startActivity(intent);
On the CapitalActivity i have this code:
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("RIGHT_ANSWER_COUNT", score);
intent.putExtra("NAME_ACTIVITY", "CapitalActivity");
startActivity(intent);
etc.....
On the ResultActivity i have this code:
activity = getIntent().getStringExtra("NAME_ACTIVITY");
public void playAgain(View view){
if(activity.equals("FlagsActivity")){
Intent intent = new Intent(getApplicationContext(), FlagsActivity.class);
startActivity(intent);
}
if(activity.equals("CapitalActivity")){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
if(activity.equals("PopulationActivity")){
Intent intent = new Intent(getApplicationContext(), PopulationActivity.class);
startActivity(intent);
}
if(activity.equals("EconomyActivity")){
Intent intent = new Intent(getApplicationContext(), EconomyActivity.class);
startActivity(intent);
}
if(activity.equals("ContinentActivity")){
Intent intent = new Intent(getApplicationContext(), ContinentActivity.class);
startActivity(intent);
}
}
Basically im sending an Intent with a String containing the name of the activity, then on the Result Activity evaluating with "if" the String = That activity name, start the activity.
What i want to do is someting like this:
On the Flags Activity:
Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra(FlagsActivity.class);
startActivity(intent);
On the CapitalActivity:
Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra(CapitalActivity.class);
startActivity(intent);
On the Result Activity:
activity = getIntent();
public void playAgain(View view){
Intent intent = new Intent(getApplicationContext(), activity);
startActivity(intent);
}
So that i can create as many quiz activities without having to create an "if" statement on the ResultActivity for it to work.
You can pass the class name as a string and use reflection to look up a class object for that type. Something like this:
Class classToLoad = Class.forName(getIntent().getStringExtra("NAME_ACTIVITY"));
Intent intent = new Intent(getApplicationContext(), classToLoad);
startActivity(intent);
I would pass the fully qualified class name to avoid errors.
You could pass the String of activity name to the Result activity, and get its corresponding class name using reflection:
String strActivity = "com.package.FlagsActivity";
Intent intent = new Intent(getApplicationContext(),ResultActivity.class);
intent.putExtra("activity_name", strActivity );
startActivity(intent);
String activityName = getIntent().getStringExtra("activity_name");
Class<?> myClass = Class.forName(activityName );
Intent myIntent = new Intent(getApplicationContext(), myClass);
Something like this.
In the Results Activity
public static void toActivity(Context context, final Class ActivityToOpen){
//whatever awesome code you would like to perform
Intent intent = new Intent(context, ActivityToOpen);
startActivity(intent);
}
In the originating Activity (Flags Activity for example)
ResultsActivity.toActivity(FlagsActivity.this, FlagActivity.class);
Check for typos... I kinda winged this :)
You should be able to call from any originating Activity without if statement.
When you receive the data
String activity;
Bundle bundle = getIntent().getExtras();
if (bundle != null){
activity = bundle.getString("NAME_ACTIVITY");
}

Avoid third activity go back to first activity

I have three activities A,B & C.
A is Started screen / B is Login screen / C is Main tab bar screen
On A, I can push to B
On B, I can push to C and go back to A
On C, I want to avoid it go back to A, but can push to A (Means logout)
A <---> B ---> C ---> A
How I can achieve that?
A.activity
Intent i = new Intent(StartedActivity.this, LoginActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
StartedActivity.this.startActivity(i);
//finish(); //if i put finish() B cannot go back to A
B.activity
Intent i = new Intent(LoginActivity.this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
LoginActivity.this.startActivity(i);
finish();
C.activity
//This code is for logout, push to A.activity
Intent i = new Intent(MainActivity.this, StartedActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
MainActivity.this.startActivity(i);
finish();
So now the problem is I can press go back to A. How can i avoid it?
A.activity
Intent i = new Intent(StartedActivity.this, LoginActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
StartedActivity.this.startActivity(i);
B.activity
Intent i = new Intent(LoginActivity.this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
LoginActivity.this.startActivity(i);
C.activity
Intent i = new Intent(MainActivity.this, StartedActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.this.startActivity(i);
and override method onBackPressed on B.activity
#Override
public void onBackPressed() {
Intent i = new Intent(MainActivity.this, StartedActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.this.startActivity(i);
super.onBackPressed();
}
so now you can apply flow A <---> B ---> C ---> A
one issue here is A will be reset when back from B
hope this helps
Register a BroadcastReceiver in Activity A, and call finish() when you logged in.
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if ("your-action-string".equals(intent.getAction())) {
finish();
}
}
};
Register it in your onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
LocalBroadcastManager.getInstance(this).registerReceiver(receiver,
new IntentFilter("your-action-string")); // define your own action
}
Unregister it in onDestroy:
#Override
protected void onDestroy() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
super.onDestroy();
}
if you want to go back to the Activity A from Activity C then you set into manifests file.
<activity
android:name=".C"
android:label="#string/appName"
android:parentActivityName=".A"/>

Unwanted duplicated activity

There is a code that sends an int 'a' from the main activity to activity B. It also starts activity B with the fade animations. However, this code creates 2 of the same activity B's and I only need 1 activity B. How can I fix this so that it only makes 1.
new Handler().postDelayed(new Runnable() {
public void run() {
Handler splash = new Handler();
int a = 1;
Intent myIntent = new Intent(MainActivity.this, Differentiate.class);
startActivity(new Intent(MainActivity.this, Differentiate.class));
myIntent.putExtra("HEADER", a);
overridePendingTransition(R.anim.fade_in_switch_fast,R.anim.fade_out_switch_fast);
startActivity(myIntent);
finish();
}
}, secondsDelayed * 2000);
You are starting the second activity twice. Remove the following line from your code and move overridePendingTransition after you use the intent to start the activity:
startActivity(new Intent(MainActivity.this, Differentiate.class));
Hey just remove this line from code:
startActivity(new Intent(MainActivity.this, Differentiate.class));
Rest is fine in your code.
That's because you are starting Activity B twice!
you should remove this part of your code:
startActivity(new Intent(MainActivity.this, Differentiate.class));

onbackpressed() from Activity to Specific Fragment

I have a MainActivity class with 3 Fragments (each in their own class)
My third fragment (LoginFragment) will allow Login a user and then go to a new activity (new Intent) with some info for that user like the product.
If I press back on that Intent will go back to the LoginFragment.
I override the #OnBackPressed to start the MainActivity:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
this.finish();
I need to know how to replace that Fragment with the LauncherFragment (Fragment 1) in MainActivity.
I have this solution but it takes 0.5 sec to 1-2 sec based on device
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
this.finish();
would be cool to go direct to Fragment 1 like to finish the third fragment thanks :)
I fixed the issue in this idea
onBackPressed() I call a new Intent but with extras
In the MainActivity that has the 3 fragments onRestart() I check if it coming from this class ( has that extras ) than go to this fragment (click,replace,delete)
#Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(Constants.Intents.NAVIGATE_BACK, true);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this.finish();
}
on the MainActivity I got this
#Override
protected void onRestart() {
super.onRestart();
Intent intent = getIntent();
boolean navigate = intent.getBooleanExtra(Constants.Intents.NAVIGATE_BACK, false);
if (navigate) {
View homeView = bottomNavigationView.findViewById(R.id.home);
homeView.performClick();
intent.removeExtra(Constants.Intents.NAVIGATE_BACK);
}
}
If you want it to be as fast as possible, use one activity and fragments to vary the contents. Adding a fragment doesn't create a new window, whereas an activity does.
Also look at your application logic in fragment / activity startup (onCreate(), onResume(), etc). That's going to be the main factor.

Categories

Resources