I'm developing a Twitter client for Android and I would like to know which is the better way to build my app.
I want to have a listview that, when clicked, calls another listview to see details and so on but I would also like to be able to go back in my previous list.
What is the best way to do this in Android? At every click, I create a new intent associated with an activity and I do startActivity(intent) so that I can be able to go back with the Back button of Android.
Is it a good way or is there a better approach? I would like to navigate onward and backward of n-levels
The way to navigate back in Android is by launching the Activity intent waiting for a result Doc link and then when you need to go back from the new Activity simply finish it Doc link
Calling finish() in an Activity closes it and returns to the previous one. You can call in on a button click or similar.
Related
I have a main activity where user provides an input according to which i jump to a activity deep inside the heirarchy eg:
Let activities be B->C->D->E
On user input it will jump from A to E.What i want to do is i want to add B C D onto the backstack so that when user press back button it navigate according to the heirarchy. Also i want to remove A from the backstack so after B app should exit. I know there are many related questions but I am not able to make out how exactly to do this. I have been following the tutorial for providing proper back navigation on android official site:Create Back Stack. In this tutorial i did not understand how PendingIntent is used or what upIntent is.I am new to android development so any help will be appreciated.ThankYou
Sounds like you need to use fragments within 1 activities framelayout. You can use FragmentManager to manage the backstack
more info:
http://developer.android.com/reference/android/app/FragmentManager.html
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
I'm having some problems with understanding the activity stack and the behaviour of how it affects my app.
Upon clicking a button it starts an Intent which opens the browser. When I'm in the Browser and I press the home button I land onto the homescreen. Now if I start my app again via launcher it opens the browser instead of my app. How can I circumvent opening the browser upon launching my app?
Right now, the code to open an url looks like this:
private void openUrlExternal(String url) {
Intent openUrlIntent = new Intent(Intent.ACTION_VIEW);
openUrlIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
openUrlIntent.setData(Uri.parse(url));
startActivity(openUrlIntent);
}
Am I using the wrong flags? If so, what flags do I have to use?
Thanks in advance!
Try like this:
openUrlIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
openUrlIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
That should disassociate the browser task from your own which means when you re-launch yours it should go to your Activity instead of the browser.
However it also depends on where you are calling openUrlExternal() from. If you call this when your activity launches it is still going to take you back to the browser, but if you call this from an event listener (i.e. Button click) then it shouldn't get called when you re-launch your app.
I don't think the accepted answer is exactly correct. It depends on what you intend (no pun intended, heh) to do.
Using Intent.FLAG_ACTIVITY_NEW_TASK it means that the launched activity is completely separate from the launching one. In particular, you can switch to the old activity with the Apps button without exiting the new one.
Using Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET means that the user will be returned to the previous activity when it's launched from the apps drawer.
In both cases launching the app again will get you to the previous activity. The main difference will be whether both activities (or just the last one) are shown in the app switcher.
In my app I have situations where I need to get a user back to some activity that preceded(not necessarilly directly) the current one. All of those previous acitivities might need Intent parameters in onCreate.
So, my question is there any easy way to get user back to an activity that might not be the direct previous activity he's been on and is it possible to avoid manual workaround of saving/restoring those previous activities' intent parameters ?
Consider an example: there's a global search-bar that can provide users with suggestions on products; once they hit one of suggested items they get moved on a product-view activity where they can reload this activity with another product - walk through. After a couple of such reloads they might decide to go back to the activity where the search was initiated, but it might not the closest to the current one.
UPD: There also should be a possibility to go back in B activities sequence.
Using startActivityForResult() while loading a new activity and using finish() to close the launched activity on back press can solve your 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.