Go to home screen from Android Activity - android

I'm making an application in android and I want to implement a button such that whenever it is pressed, I just reach back to my home screen. I know that we have the hardware key and soft keys( when there are no hardware keys) which implements this, but I want to add this functionality for this application. Does anybody has any idea how to do it?
Thank you

Try this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

You can use an icon on your action bar and program it to bring you back to your application home screen.
Take a look at this section of the developers guide.
http://developer.android.com/design/patterns/navigation.html

The Android architecture is not ready to exit an Application with one line of code. You just cannot do it. Rely on your hardware buttons or make a finish() waterfall effect on all your Activities. You can use startActivityForResult() to start your Activities (all of them if you want this method to work). Then, when you want to exit your App, just call setResult(Activity.USER_CANCELED); and finish(); right after it. It will return to your previous Activity onActivityResult() callback. There, if the requestcode is the right one and the resultCode is equal to Activity.USER_CANCELED, just do the same: Call setResult(Activity.USER_CANCELED); and finish();. Once more, it will take you back to the previous Activity, if it exists. And so on, until you exit your app.

Related

Up navigation of external app to return to calling app

I'm letting users access the device Settings via a menu within my app:
Intent dialogIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);
startActivity(dialogIntent);
However, once user is done with making changes in Settings, I need them to be able to return to my app. We made modifications to Android to remove the soft buttons, so using the standard back button is a no-go.
Is there a way to enable the standard back button to go back in history or to go up from Settings back to my app?
Thanks!
Calling finish() from the second activity should return you to the previous one (unless you have used android:noHistory = "true" in the manifest). In case you want to take back some data from the second activity to the first one, you can use startActivityForResult instead. You can read more about the latter idea here, if you need.
Developer.Android link to startActivityForResult

Android up navigation for an Activity with multiple parents

I have a problem for implementing up navigation on an app with this navigation tree:
The standard implementation of the back button is fine.
The problem start when trying to implement the Up button.
What I expect:
when the user is on Detail 5 Activity and press the up button the app goes to List 3 Activity
when the user is on Detail 7 Activity and press the up button the app goes back to Home Activity
So in different terms, I'd like to have this behaviour on the back stack:
The Android documentation (Implementing Ancestral Navigation) advice to use the following code to handle up navigation:
Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();
But because the parent activity of the Detail Activity differs on the different navigation path I don't know which one it really is. So I can't call it in the Intent.
Is there a way to know the real parent activity in the Android back stack?
If not, is there a way to implement a correct up navigation in this app?
I will stick with my comment on Paul's answer:
The idea is to have a Stack of the last Parent Activities traversed. Example:
public static Stack<Class<?>> parents = new Stack<Class<?>>();
Now in all your parent activities (the activities that are considered parents -e.g. in your case: List and Home), you add this to their onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
parents.push(getClass());
//or better yet parents.push(getIntent()); as #jpardogo pointed
//of course change the other codes to make use of the Intent saved.
//... rest of your code
}
When you want to return to the Parent activity, you can use the following (according to your code):
Intent parentActivityIntent = new Intent(this, parents.pop());
parentActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();
I hope am right (:
That's a tricky question and in my opinion really shows the difficulties in coping with the UX decisions of Android for the "up button". Therefore, there's not a clear-cut answer to your problem.
I have two possible solutions for you.
1. Mimicking the back button behavior.
You could consider adding an extra to the intent for launching Detail from one of its various parents. This extra would inform those activities which activity they would need to launch when android.R.id.home is pressed.
This would effectively mean that your app "goes back" to its common ancestor, instead of simply relaunching Home.
Another way of implementing this may be simply executing onBackPressed() instead of launching Home with Intent.FLAG_ACTIVITY_CLEAR_TOP, but bear in mind that the associated animation would be different than a normal "up" action.
2. Skip intermediate activites and go home.
Some apps treat the "up button" as a "home button". You might want to consider having it simply always relaunch Home with Intent.FLAG_ACTIVITY_CLEAR_TOP.
this is an old post for sure, but as I was studying the SharedPreferences, I think it could be a possibility to stack this information within a sharedPreferences data, and to modify its value each time before going down the 2 parents.
Then by reading it, you should be able to directly know your parent, and this without having to build a whole class for that.

Close the whole stack of activities in Android without graphical issues

I am trying to close a whole stack of activities using the way described here:
How to kill an application with all its activities?
Namely, each activity starts the other one with startActivityForResult, and in onActivityResult calls finish() to close itself together with the activity it opened.
The problem is that the activities in the task still seem to repaint themselves at least once before they close, and this doesn't look good. After closing the topmost activity one sees all previously opened activities like in a very fast slideshow.
How can one avoid this graphical issue?
EDIT: I need that if the user presses HOME button and then presses the app's icon in launcher, he returns to the current state of the stack, not to the very first activity again. So, from what I understand, with such a requirement I can't finish() activities before starting next ones.
That's native behaviour, intended to aid in user Experience. When an Activity is started with startActivityForResult and then finishes, it will (on devices that allow fancy animations) automatically slide away. That helps people not get surprised by the screen suddenly changing.
You could try starting the Activities without startActivityForResult and handling the passing of data to and from Activities manually, then handle how/when Activities finish() and which Activity they pass back to. You might find you implement something where Activities actually pass forward to the appropriate Activity all the time, rather than back to an Activity on the stack.
Intent intent = new Intent();
intent.setClass(getApplicationContext(),
PhoneListCheckboxAES.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
If u give like this when u are starting the next activity then the graphical problems won't occur

android Intent problem

Actually i m little bit confused in Intent.
Suppose i have three activities.
A,b,c and in activity A i have exit button. When i click on exit button my application finishes. I have one more button in A which is next button. Which take me to new activity.
and in activity B i have two buttons next and back, and in activity C also i have two button out of which first takes me to A and Back button.
now i'm on C activity and want to go to A. where when i press exit it again takes me back to C instead of finish the application.
Why is this happening?
Not really answering your question but your Android application just shouldn't have an Exit button. It's not necessary.
This blog post by Reto Meyer - a Google employee who works on Android - explains it well. This passage from it might be significant in relation to your problem:
In most cases the exit button simply calls Activity.finish. This is exactly equivalent to hitting the back button. Exactly.
There is no Exit function in Android.
You probably want to bring up the Home application by it's corresponding Intent:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Exit button or not, Activity.finish only applies to the current activity and you are dealing with three different activities. Finishing activity A is simply taking you back in your stack to the previous activity C.
Check out the documentation on Activities and Tasks, launch modes, and clearing the stack for some explanation of what's going on in your example and what you can do to alter the behavior. I've always thought these sections of the Android documentation need to be enhanced or further explained but hopefully it will help a little.

Displaying app main menu/home screen when returning to app after having started an external Activity/Intent

I'm starting Android Market via my app to search for similar products using this code:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://market.android.com/search?q=pub:\"some txt\""));
c.startActivity(intent);
This works fine for showing similar products. However, if I hit the home button while in the market, when I re-open the app it still shows market results. I want to go to the main menu in this case.
Is there a solution?
Sorry, FLAG_ACTIVITY_NO_HISTORY is probably not the correct solution. Note the semantics of it -- the activity just doesn't appear in the history. Thus if the user taps on one of the things in it to go to the next activity, then pressing back, they will not return to the previous one (but the one before). This is rarely what you want.
Worse, if they go to a second activity from the market activity, press home, and return to your app, the second activity will still be there (it is keeping itself in the history).
The correct flag for this situation is FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET.
If you add the FLAG_ACTIVITY_NO_HISTORY flag to the intent, it won't be kept on the history stack. When the user navigates back to your application, the last activity that was visible before you launched the marketplace will be shown.
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://market.android.com/search?q=pub:\"some txt\""));
c.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
c.startActivity(intent);
Edit: hackbod is correct: FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET is a better fit for what you need.
This is not a problem.
When you press home on the Market app it isn't closed, just paused. So when you open it again you resume it. Check Android activity's lifecycle.

Categories

Resources