I have 3 activity, I want to return to the first activity and Refresh it after clicking on the button at second and Third activity, without using "new Intent(...)"!
I use this code:
ActivityFirst actFirst = (ActivityFirst) _context;
actFirst.recreate();
but it doesn't work! with Error : java.lang.ClassCastException
Is there any way to refresh first activity from second or third activity without use below code?
Intent intent = new Intent(this, ActivityFirst.class);
startActivity(intent);
You cannot refresh your first Activity because the thread is paused when the Activity is not in the foreground. Also the reason you are getting a ClassCastException I'm assuming is because your trying to cast your second or third Activity into the first Activity by passing your current Context.
You should just refresh your first Activity inside onResume like so:
#Override
protected void onResume() {
super.onResume();
recreate();
}
To start your first Activity again you could call this.
Intent intent = new Intent(this, FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
If you want to finish second or third activity when you click on button on these activity then just use onActivityResult in First Activity and refresh you first activity.
If you only want to refresh without finish second ot third activity then send broadcast message to first activity.
Let me know this is useful for you?
Go through Activity Life Cycle, When you have opened the 2nd activity the first activity will be in pause state.so while resuming the activity you can refresh the activity.Try these method to implement it : onResume()onPause()
startActivity(new Intent(B.this,A.class));
finish();
If you pass Intent from second activity(B.this) to first activity(A.class) ; it will be recreate that first activity. When you press back than not second activity open because of finish();.
Related
My Previous Activity gets Destroyed on view-pager page changed(of running Activity). Thus onBackPressed() my App gets closed. I don't want to recall previous Activity explicitly.
Assume FirstActivity then open SecondActivity which has one viewpager . When we change page in view pager in Second Activity then FirstActivity (which is in Background) gets destroyed. so when we come back and back press on SecondActivity application closed
Check whether you have called finish(); after starting the ViewPager activity.
Intent intent = new Intent(getBaseContext(), SomeActivity.class);
startActivity(intent);
finish(); // This will destroy current activity.
You should not use finish() method for your activity while starting a new Activity explicitly. Instead, you should handle your activity every time in onBackPressed() method. if you manage your activities using lifecycle states i think you will not need for further explicit call to your activity.
I have an activity. After checking some stuff, I want to go back to previous activity if a previous exists, and if not I want to start a SpecificActivity. How do i do it?
Edited:
Seems like no one is understanding what I meant, so let me rephrase. Lets say I am in Activity A. I don't know if A is the only activity in the stack. If there are other activities, then I want to finish A and pop the activity right below A in the stack into the foreground. If A is the only activity in the stack, I want to start some activity Z. How do i do it?
You have to pass class name as intent extra from both Splash and DashboardActiviy.
In List Activity you have to get the class name using getIntent().
When the user click back button, you need to check the class name based on that you can take decision.
if(name.equalIgnorecase(DashboardActivit.class.getSimpleName()){
//Add your intent
}else{
//
}
This may give you definite solution to you.Give a try
you can simply override the onBackpress()
#Override
public void onBackPressed() {
super.onBackPressed();
startActivity(new Intent(this, Destination_Activity.class));
finish();
}
call the next activity like.
Intent intent = new Intent(this,Your_Next_Activity.class);
startActivity(intent);
then it will call your another activity and your current activity will be on background if you will use finish() after calling next activity it will finish your current activity so don't use finish() after calling your next activity in this scenario.
After that when you press back button it will automatically finish current activity.
I want to go back to an already launched activity without recreating again.
#Override
public void onBackPressed() {
Intent i = new Intent(SecondActivity.this, FirstActivity.class);
this.startActivity(i);
}
Instead of doing is there any other way to go back to another activity which is already created, which I get back with " Back button "
Add android:launchMode="singleTop" in the FirstActivity declaration in your AndroidManifest.xml like so
<activity
android:name=".FirstActivity"
android:launchMode="singleTop"/>
You could also try the following if SecondActivity was started from FirstActivity
#Override
public void onBackPressed() {
this.finish();
}
Whenever you start a new activity with an intent you can specify an intent flag like this:
// this flag will cause the launched activity to be brought to the front
// of its task's history stack if it is already running.
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
or
// If set and this intent is being used to launch a new activity from an
// existing one, the current activity will not be counted as the top activity
// for deciding whether the new intent should be delivered to the top instead
// of starting a new one.
intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
When starting SecondActivity do not finish the first activity so when you go back , you will be taken to the first activity.
Intent i = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
On your second Activity, close the Activity calling finish() instead of creating a Intent for the other screen.
By calling finish() the Application will go back in the Activity stack and show the previous Activity (if its not finished()).
Don't call finish() on 1st Activity, when you start 2nd Activity. When back is pressed on 2nd Activity, it finishes itself and goes back to 1st Activity by default. onCreate() is not called for 1st Activity in this case. No need to override onBackPressed() in 2nd Activity.
However, Android can destroy 1st Activity when you are using 2nd Activity, to reclaim resources, or, if you have checked Don't keep activities in Developer options. In such case you can use onSaveInstanceState() to save data and onRestoreInstanceState() to use it again in 1st Activity. You can also get the saved data in onCreate(). Here is how it works. Also, onSaveInstanceState() is called only if android decides to kill the Activity and not when user kills the Activity(e.g. using back button).
I'm navigating from:
Main activity to Activity 2
Activity 2 to Activity 3
Activity 3 to Activity 4
through Intent.
I've also created the menu so that user can directly navigate from Activity 4 to Main activity. But after navigating from Activity 4 to Main activity by using menu, when I press back, it takes me to Activity 3 rather than exiting the application.
I tried:
#Override
public void onBackPressed() {
super.onBackPressed();
MainActivity.this.finish();
}
But no gain. Any suggestions?
Just try this code when you navigate from 4th Activity to Main Activity.
Intent inMain=new Intent(Activity4.this, MainActivity.class);
inMain.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(inMain);
Note:
This code in your case, clears the previous activities and launches the main activity with no activity in the backstack.
You can set flag Intent.FLAG_ACTIVITY_CLEAR_TOP to finish all the intermediate activities and your called activity will come to top of the activity stack.
use the flag Intent.FLAG_ACTIVITY_CLEAR_TOP with your intent. For more detail -
FLAG_ACTIVITY_CLEAR_TOP
call startActivity Method using clear top flag
startActivity(new Intent(this, UI.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
You need to consider your "back stack" more closely.
What is happening exactly is, your back stack gets populated as follows:
Main activity -> Activity 2 -> Activity 3 -> Activity 4
Then from Activity 4 you launch your Main Activity. So your stack becomes:
Main activity -> Activity 2 -> Activity 3 -> Main Activity
Hence, when you press back, you land up in Activity 3.
Solution:
Either call finish() on every Activity when you navigate away from them. Or use Intent.FLAG_ACTIVITY_CLEAR_TOP to clear all intermediate Activities.
More about Intent.FLAG_ACTIVITY_CLEAR_TOP.
When you go back to the MainActivity you need to use the Intent.FLAG_ACTIVITY_CLEAR_TOP on your Intent. This is an example of a goHome method that you can used in your Activity:
public void goHome ()
{
Intent homeIntent = new Intent();
homeIntent.setClass(this, MainActivity.class);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
In activity 2 and 3, after calling startActivity(intent), call this.finish(). This will also be one solution.
Finish previous activity when you are go to the next activity means write finish(); after startactivity(intent); and write below code for start first activity from fourth activity's button click event.
Intent in1=new Intent(Act4.this, Act1.class);
startActivity(in1);
finish();
I am having application in which i am using menu , on tap on menu item i am redirecting to specified activity.
I want to make sure that when i am redirected to another menu item my current all activity should be finished to reduce the stackflow of the activity and better performance.
So when i tap on back from my tapped activity from the selected menu activity i should be redirected to another activity and finish current activity.
So i am wondering is there any way by which i can finish another activity from my current activity. Or should i override the OnKeyDown Method..
Any help on this
Thanks in advance
You can do:
Intent intent = new Intent(....) ; // intent to launch the new activity
// fire intent
finish(); // finish current activity
Intent i=new Intent (currentclass.this , nextclass.class);
startActivity(i);
finish();
Use finish() as shown above ,and here we are redirectin to nextclass.class so all activity of currentclass should be finished to reduce the stack over flow ..
and mind it finish() calls onDestroy() .and onDestroy() is executed...and destroy current activity(in my case currentclass)..
also mind it. onDestroy() isn't a destructor (as in c++) .It doesn't actually destroy the object of current activity... That's it....