I've been tweaking a lot with android design and capability of Android to use the maximum potential of an Android framework, I have come across transition, and my question is how to define Activity's Destroyed's animation ?. Say I am starting an activity using intent like so :
Intent intent_info = new Intent(ComponentsPage.this, SecondActivity.class);
startActivity(intent_info);
overridePendingTransition(R.anim.slide_up, R.anim.no_change);
That snippet basically opens up SecondActivity with Slide Up transition. Now i am in second activity and second activity say doesn't have any button but i want whenever Second Activity is closing (Destroyed) it close with slide down animation.
I've tried adding
overridePendingTransition(R.anim.no_change,R.anim.slide_down);
Inside onDestroy() and onStop(), but still no luck, i guess the activity is already closed when those methods are called.
When you try to finish the second activity try to override the finish method inside your SecondActivity as follows:
#Override
public void finish() {
super.finish();
overridePendingTransition(R.anim.no_change, R.anim.slide_down);
}
Related
Let me explain detailed; I have notification and this notification opens B activity with two cases.
Cases :
If app is closed. (not running on background)
If app is opened. (on background or front)
Case-1
I click to the notification and it opens the B activity with case-1. When i press back i want to go to the A activity and kill B activity. I dont need B activity anymore. Everything easy from here without using flags. When I'm on B activity and press back two times from here, it goes A activity and then closes the app. My trouble here is, if i open the app from navigation buttons of phone (can't remember the name of this button) app is opening from B activity. That's not what i expected. I want to open A activity. Don't want to see B activity anymore.
Case-2
I click to the notification and it opens the B activity with case-2.When i press back i want to bring A activity to the front, without creating anything new. If i press back on B activity, two times and close the app and then again re-open app from navigation button of phone, want to open app from A activity.
So how can i make this correctly, i tried to use flags (i already read docs) but couldn't get work.
What flags should i use when i open the B activity and onBackPress method of B activity to go A activity as i wanted
This should be achievable by adding
android:launchMode="singleTask"
to the A activity in the Manifest, then you can just open A activity from B activity onBackPressed and you will have A only once in the stack.
If it's not working the way you want, you can create an abstract class that extends Activity and handle the stack in a static object, then A & B must extend this new class
try this
Intent intent = new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
try this
android:launchMode="singleTask" in android manifest file
You can achive this by adding FLAG_ACTIVITY_REORDER_TO_FRONT
Intent i = new Intent(context, Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
You May try this isTaskRoot() Which will return B is root
if it is true then launch A
other wise you may finish B
B Activity
#Override
public void onBackPressed() {
if (isTaskRoot()) {
//call A which is not exist
Intent i =new Intent(B.this,A.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}else {
//Finish B if A Already Exixt
finish();
}
}
You can call B Activity on Notification Click
if A is present then u can finish B else you can launch A
If set FLAG_ACTIVITY_CLEAR_TOP, the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
Just put this in ActivityB onBackPressed:
Intent i = new Intent(ActivityB.this , ActivityA.calss);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
finish();
How solve case 1:
finish(); on ActivityB BackPressed method make ActivityB finish after open ActivityA. So after opens ActivtyA, ActivityB will shut down.
How solve case 2:
With this combination flag, It will do what you want. It will close all activities in stack and just keep destination activity. If instance of activity exist it will use it and calls OnNewInstance and if not it will creates new one.
If this is the only instance of Activity B being used, you can add the flag noHistory to the manifest for Activity B
android:noHistory="true"
This will stop Activity B being added to the back stack, this is also possible dynamically by using the Intent Flag FLAG_ACTIVITY_NO_HISTORY when calling Activity B.
As for having Activity A start when Activity B is killed #Quentin Menini's answer of having a single task activity set in the manifest will work if that is the only way you wish Activity A to be accessed, or the Intent Flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT as #Naimish Vinchhi has suggested, will have the desired effect in this instance.
https://developer.android.com/reference/android/content/Intent.html
link to see all possible Intent flags
https://developer.android.com/guide/topics/manifest/activity-element.html
link for all possible manifest activity options
If I'm bringing Android activities from the stack to the front, how do I refresh them? So to run onCreate again etc.
My code below, in conjunction with setting activities in the Android manifest to android:launchMode="singleTask" allows me to initiate an activity if that activity is not already active within the stack, if it is active within the stack it is then brought to the front.
How do I then, if the activity is brought to the front refresh it so that onCreate is ran again etc.
Intent intent = new Intent(myActivity.this,
myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
I think FLAG_ACTIVITY_CLEAR_TOP will resolved your problem:
Intent intent = new Intent(myActivity.this, myActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I don't think there is an explicit way to refresh onCreate, perhaps you may want to add the code you want reloaded into onResume.
This workaround may work if you want to keep your code in onCreate.
finish();
startActivity(getIntent());
If all you want to do is refresh the content, you should override the onResume method, and add in the code to perform the refresh in this method. To do this, use the following code within the activity that you want to perform the refresh, (ie, not the same activity that you are calling startActivity() from):
#Override
protected void onResume(){
super.onResume();
//add your code to refresh the content
}
Tip: If you are using Android Studio, press Alt+Insert (while you have the Java file open), then click Override Methods, find onResume, and it should provide you with a basic template for the method.
The diagram I added shows the order that the methods are run (this is known as the Activity Lifecycle). onCreate() is run whenever an Activity is first created, followed by onStart(), followed by onResume(). As you can see, when a user returns to an Activity, onCreate() is not run again. Instead, onResume() is the first method that is called. Therefore, by putting your code into the onResume() method, it will be run when the user returns to the activity. (Unlike onCreate(), which will not be run again).
Extra info: Since you will be initially setting the data in onCreate() and then refreshing it within onResume(), you might want to consider moving all of your code used to initially set the data to onResume() as well. This will prevent redundancy.
Edit: Based on your following comment, I can give the following solution:
I'm wanting to properly refresh the page, e.g. if there is a variable count initialised at 0. And though running the activity it's has became equal to 300. When the activity is called (intent) then refreshed, count will once again be equal to it's initial value. Do you know how to do this?
Without your current activity's code, there is not much to work with, but here is some pseudo-code as to how I would accomplish your problem:
public class MyActivity extends Activity {
TextView numberTextView;
int numberToDisplay;
#Override
protected void onCreate(Bundle bundle){
super.onCreate(bundle);
setContentView(myContent);
numberTextView = (TextView) findViewById(R.id.numberTextView);
numberTextView.setText(numberToDisplay+"")//converts the integer to a string
}
#Override
protected void onResume(){
super.onResume();
numberToDisplay = 0;
numberTextView.setText(numberToDisplay+"");
}
}
Old Title: Continue execution of code from activity 1 only after activity 2 finish()
Let's consider this code:
// Call activity to take some pictures
Intent i = new Intent(MainAct.this, CameraAct.class);
startActivity(i);
// After CameraAct is closed (finish()) then send pictures to webservice
sendPicturesToWebService();
I know this code is wrong because after run startActivity(i) the code will not wait until that new activity finishes to run sendPicturesToWebService() like if the new intent was a model window/screen.
How can I get this done?
by 'this' I mean: create and show a new activity as if it was a modal window / a dialog window; and continue to execute code from old activity right after the modal activity is closed.
you have to startActivityForResult() and then handle the "result" on the onActivityResult callback. Here the link reference to the official docs:
startActivityForResult
onActivityResult
I'm accustomed to using an intent to launch a new activity when a button is pressed.
But the app that I'm making uses a list view, which means I want to be able to go back from my searching activity to the main activity by using the phone's back button.
I was reading and experimenting with different types of android methods, this one in particular which seemed simple but doesn't work, or perhaps I'm doing something completely different.
public void onBackPressed()
{
Intent setIntent = new Intent(this,MainActivity.class);
startActivity(setIntent);
finish();
}
Doesn't Android's onBackPressed method respond to any android phone's back button?
Thank you for your help.
You forgot to call super.onBackPressed() inside onBackPressed() method, you dont need to start a new intent to go back.
In your search activity override the onBackPressed() method and call it from wherever you want in the activity.
it should be like this.
#Override
public void onBackPressed(){
super.onBackPressed();
}
Normally In an Fragment, implicit intent can be used to start a brower component,if there are several brower component, the Android system will show an selection dialog ,the dialog just overlay the Fragment which send the intent.
but in my situation when selection dialog is shown ,the Fragment go to the background(disappeared) ,and the home screen can be seen.
does anyone encounter this problem ,how to make the Fragment not go to the backgoround.
in onPause method it just look like below in my fragment source
#Override
public void onPause() {
mLog.printMethodLifeCycle("onPause");
super.onPause();
}
and in activity I did not overide the onPause method
AndroidManifest.xml is too long ,can you tell me which part should I post or notice