I have 3 Acivity based application, It's work flow like this MainActivity ListView and DetailView. when onResume event trigger , need to call MainActivity. without going to other two activity.
Is there any way to call MainActivity when onResume event trigger?
Thank You
You can set the android:clearTaskOnLaunch="true" attribute for you MainActivity in the AndroidManifest.xml file. See here to find why and more details. I think this is the most convenient way to meet your demand.
Edit:
I just tested and found this only works when you exit the app and launch the app from the app drawer(NOT long press on HOME and select the app).
If you want to always bring the root activity to the front, no matter when you re-launch the app or from the recent screen. You can declare "android:launchMode="singleTask" for the root activity, here, the MainActivity.
The best solution I can think of is to start the activity again in the onResume of all your other activities:
#Override
public void onResume() {
Intent myIntent = new Intent(this, MainActivity.class);
startActivity(myIntent);
}
The user will still be able to hit the back button and go back to the previous activity, however.
If you want to quit your List/Details views when the user closes your app, have them finish() themselves in their onPause which is called when your Activity is closed.
The only caveat here is that calling finish() will move it one Activity back in the ActivityStack so if your MainActivity isn't the one launching the List/Details views, it will not go back to the MainActivity. In this case, you could specify in the AndroidManifest.xml
<activity android:name="sample.activity.MyActivity" android:noHistory="true" />
to prevent the List/Details activities from ever being put into the history.
Related
I have four activity for my application, Main Activity -> second Activity-> Third Activity -> Fourth Activity. Each Activity contains the onBackPressed method implemented. Every time when the onBackPressed method called, it sends an intent to the previous activity. For Example, in Fourth Activity onBackPressed method, it contains an intent to go to the previous Activity, which is the Third Activity. This works fine upto the MainActivity. When I press on the back button on the MainActivity it must exit the application. However, it just starts the next activity, which is the second Activity. What causes this bug???
Here is some relevant code.
From the fourth activity:
#Override
public void onBackPressed() {
Intent intent = new Intent(this,third.class);
intent.putExtra("id",FinalID);
intent.putExtra("Image Item", categoryItem);
startActivity(intent);
}
From the third activity:
#Override
public void onBackPressed() {
Intent intent2 = new Intent(this, second.class);
intent2.putExtra("Image Item", categoryItem);
Toast.makeText(this, "theFinalActivity=" + categoryItem.getId(), Toast.LENGTH_SHORT).show();
startActivity(intent2);
}
The second activity looks like the following.
#Override
public void onBackPressed() {
next = false;
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
And finally the code in the MainActivity:
#Override
public void onBackPressed() {
System.exit(0);
}
Android System internally handles the backstack for the activities. You don't need to explicitly handle going back if you're not supposed to override the default behavior. In your case, On pressing back, you're not popping the current activity, instead you're pushing a new one into the backstack. This will result in ambiguous behavior of your back button.
First, the onBackPressed function is not required to be implemented only if you are caring about taking care of the activity stack (i.e. finishing up your third activity will bring back the second and finishing the second activity will bring up the first one and so on). The activity stack is already taking care of it.
Secondly, if you want to pass the data to a caller activity from the activity that you have opened from the caller, this is not the ideal way to do so. I would like to recommend you to read this documentation where it explains how to pass such intents in a graceful manner using the life-cycle functions provided by Android.
Just to give you a heads up, here are three steps for you to pass the data between activities in a backward direction.
You need to start the second activity from the first activity using startActivityForResult instead of startActivity with a request code passed along with it (so that you can differentiate between different request codes from other activity if there is any).
Then in your second activity, if you need to pass something to your caller activity (i.e. the first activity), you need to use setResult to pass the desired data back to the caller and finish() the second activity.
In the first activity, you need to implement onActivityResult function to get the data back from the second activity.
Here is a nice example of the overall implementation where you can take a look too.
I know that you are expecting that the system.exit(0) should kill the application whatever the situation is. However, in Android, it does not work in the way that you have expected I think. I am quoting from this answer on StackOverflow to explain how the system.exit(0) works.
System.exit(0) - Restarts the app with one fewer activity on the
stack. So, if you called ActivityB from ActivityA, and System.exit(0)
is called in ActivityB, then the application will be killed and
started immediately with only one activity ActivityA.
I hope that helps!
I have some kind of a login activity which returns to main if login successfull. But after that, if user presses hardware "back" button, main activity is restarting for some reason...
It seems like it is going to previous (login) activity, but since it has already finished, parent activity is restarted.
I have tried using <activity android:noHistory="true"> on login activity in manifest. startActivityForResult() is not a solution for me as well. Problem remains.
Can you help me with any other ideas?
UPD: I am calling finish() when going back to main activity
This is probably because you haven't finished your activity when you're 'returning' to main. Check where you create your intents, always finish activities you do not want to be accessed via the back stack.
try use this after each startActivity()
startActivity(intent);//you already have this
finish();//add just this .. and report
Do not call finish() inside of onBackPressed() method. Android / OS will do it for you automatically.
Call finish() only If login was ok and you want to close the login activity. Of course only if main activity present in the back stack.
If back stack contains no main activity, so open it by using of common startActivity method. Also check this answer to remove an activity from the back stack.
Problem is solved by forcely exiting the whole application in main activity's onBackPressed() method. Although, it doesn't seem like a clear solution to me.
I am making a game for Android, but I never really found a good way to change the activities, or the content views. I have 1 main menu activity now, whose content view receives MotionEvents and dispatches them to my custom buttons. Another Activity has a simple contentView which just paints the screen in one color. All contentViews have the same base class and activity 2 is derived from activity 1.
The problem is, that the app just crashes if a try to change the activity. It takes about 20 seconds, then the error message appears that says the app isnt responding.
In logcat, theres also a message keyDispatchingTimedOut sending to activity2
Below is the code for activity change:
public void changeActivity() {
Log.d("changing", "activity");
Intent i=new Intent(this, Activity_Level.class);
startActivity(i);
}
Any ideas?
You have to use context of your activity in method
Intent i=new Intent(Youar_Activity_Name.this, Activity_Level.class);
^^^^^^^^^^^^^^^^^^^^^^
Use above code in your changeActivity() method.
if the app isn't responding , it's because you do a long operation on the UI thread . maybe after calling this function you continue to do something else ?
if , as people said , the activity isn't opened (and you can check it by writing to the log inside the onCreate method) , check the manifest.
in any case , if you want to have better control of activities , you can check the possible flags to use for the intents , and you can also use fragments (when possible) , just like google recommends .
Without seeing the logcat message, I bet you forgot to add the activity in AndroidManifest.xml
I sense that you didn't add all the activities to the manifest.xml file. Try to add all the activities there, and give it a run.
I have an app that circles around the main activity (a main menu). In each other app there is an option menu item that directs to this activity.
At first, I always started a new main activity when this item was selected. Using the intent bundle, I did tell the main activity that some initializations I do on a fresh start were not necessary.
However, I didn't quite like the overall behavior. I stumbled upon android:launchMode="SingleTask" and this seemed to help: now I don't recreate my main menu activity all the time; also, if I press the "back" button I come back straight to the home screen. This feels quite nicely like a proper "main" menu.
My problem now is this: if I run another activity of my app, press home button and then reopen my app (e.g. using "last apps"), then I don't go back to the last activity, but to the main one. The other activity is destroyed.
Any ideas how I can implement the behavior of SingleTask without only being able to return to one activity?
If your other activities are declared normally with activity defaults in Android, then going back to your app should take you to the same activity where you left off (using the hardware home button)
However remember that the Android system kills applications when it requires system resources. So your app may have been killed when you went to the other application. Then when you get back to your app, the default launcher activity will be restarted, which is your Menu activity.
To get back to the main activity from any activity, do this:
public static void goHome(Context context) {
final Intent intent = new Intent(context, HomeActivity.class); //give name of your main activity class here
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
}
That will clear the activity stack and get you back to your main activity. As you declared singleTop, it will bring the existing main activity to the foreground. The flag Intent.FLAG_ACTIVITY_CLEAR_TOP will remove all activities in the stack on top of the main activity. (I am assuming you are within the same application).
Now, all your other activities only need to include a button whose click listener invokes the method goHome();
From your main activity, if you press the hardware back button, it should exit your app.
Why not call finish() on the activities that were created by the main activity? This way you return to the main activity, without creating a new one...
I think you should save the state of you activity before starting another activity, and then resume your activity whenever you come back on last activity.
see Activity Life cycle from Android
http://developer.android.com/guide/topics/fundamentals/activities.html
I have a main activity which calls a child one via
Intent I = new Intent(this, Child.class);
startActivityForResult(I, 0);
But as soon as Child becomes visible the main activity gets its onStop and immediately after that onDestroy method triggered. And as soon as I call finish() within the Child activity or press the back button, the Child activity closes and the home screen shows (instead of the main activity).
How can I prevent the main activity from being destroyed? :\
If you launch a child Activity from which you expect return data, you'll probably want to use startActivityforResult instead.
You may want to check this question: Child Activity in Android as it seems to be the same problem.
Edit:
As for what's happening here, you could place code in the onStop() and/or onDestroy() methods to investigate - at least a call to isFinishing() to check why the Activity is being destroyed.
You should also use adb logcat from your host machine to check the logcat in case it holds more information - and maybe use Log.d() (the result goes into logcat as well) instead of toasts to make sure you don't miss them.
I used Dialog instead of an Activity and everything worked well so I'm leaving it like that.
check androidmanifest nohistory=true and that made the OS destroy the activity before the result. that might be one of the reason for your problem.