Control Back Navigation in android? - android

I have a search feature with a list view. When the user click on search and filters the result the activity is loaded from the beginning. So here is the flow.
User opens my application (Gets the whole list ot items) -> Search1 (Filtered search) -> Search2 (Filtered result 2) -> Search3 (Filtered result 3). Now when the user clicks back he goes to Search 2 -> then back to result 1 -> then back to the mail list.
This is how is my application working now. I want this to change as when users is on any search result I want him to go back to main list without going back to any search results.
Basically I dont want android system to track the search results so that when user clicks back, he should directly go to the main list.
How do I do this. Please advice if you have any alternative for this. Thank you for your advice and time. Thank you.

You should set your search activity to be android:launchMode="singleTop" in your manifest, that way there's a single search activity and you're only responding to search intents within it. This is actually the "ideal" behavior according to the docs.

When you are done with the search results you could finish that activity. You might even be able to use finishOnTaskLaunch to achieve the same thing and it is probably more desirable too.

Related

How to override android:launchMode="singleTop"?

I have a SearchActivity that is defined with android:launchMode="singleTop" in the manifest.
My SearchActivity includes a SearchView for the user to enter their query and a ViewPager to show the search results.
I chose single top for SearchActivity, so the use can make loads of searches in the activity and then only has to press the Back button once to get back to the home screen.
However, I have just introduced a "SmartSearch" button into the activity which, when pressed, will trigger an automatic advanced search with the results still shown in a SearchActivity.
The requirement, though, is that I need these advanced search results to show up in a new activity on the stack - i.e., so the when the user presses Back, they will be taken to the previous (standard) results screen, and then must press Back a second time to get back to the home screen.
I've read this about launch modes and this about tasks and back stack, and have tried launching the 'advanced' SearchActivity using an intent like this...
smartSearchIntent.putExtra(SearchResultsActivity.QUERY_EXTRA_KEYS, selectionArgs);
smartSearchIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
...and I also tried using Intent.FLAG_ACTIVITY_NEW_TASK as the parameter, but when I press Back on the advanced search results screen, it just jumps straight back to the home screen.
All the while, I have android:launchMode="singleTop" defined in the manifest for this activity because that is the 'normal' behaviour - so is it possible to override this and, if so, how?
Or is there a better solution?
I worked out a solution / workaround for this. Have just posted my answer for someone with a related problem, here - https://stackoverflow.com/a/27125107/1617737 .

Android app - delete item from list following an action in another activity

I'm creating an app where I display a list of pending challenges. When the user clicks on a challenge, he can accept it or ignore it.
Here's what I want to do and I don't know how :
if the user accepts or ignore the challenge, call this.finished and remove the challenge from the list
if the back button is pressed, do nothing, the challenge is still visible
In short, if the user really responds to the challenge I don't want it to be displayed in the list, but if he doesn't choose any option and press the back button, he didn't choses one of the two actions so I want that challenge to still be visible in the list.
I don't think it's possible to detect what button I've pressed when i go back to my main activity. I've thought about using global variables, but I don't want to misuse them either.
Just to be clear, I'm not asking how deleting a list item. But when to know deleting one depending of the actions of another activity.
Give your second activity the index you want to remove as a parameter inside the intent and let it finish by returning the index again as an intent extra (by using setresult(Intent i) and then calling finish) inside your first activity catch the result from your second activity by overwriting onActivityResult (http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent))
see 3.3. Retrieving result data from a sub-activity in http://www.vogella.com/tutorials/AndroidIntent/article.html for a detailed howTo

Creating a less linear flow of activities

I'm new to android development, and have set myself the task of creating a contact management application - just for the purpose of improving my skills.
The user has all the usual contact editing options available to them - find contact, new contact, edit contact, delete contact, etc. Each of these works in their own right - 'find contact' will 'select' the appropriate contact (store their ID in a public variable). If you press 'edit' or 'delete' whilst a contact is 'selected' then those functions work as they should.
But what happens when the user tries to press 'edit' or 'delete' when there is no contact selected? The simple solution would be to display the message 'please select a contact', but that is bad design. If the app needs user input, then they should be re-directed to the necessary activity to select a contact.
So when the user clicks the 'Edit' button, I wrote this very small snippet of code:
public void findContact(View view)
{
Intent intent = new Intent(this, FindContactActivity.class);
startActivityForResult(intent, 1);
}
public void editContact(View view)
{
if (Db.contact == null)
findContact(null);
else {
// navigate to edit activity
}
}
In this case, 'contact' is an object of type contact, which stores contact related information in it's own class. Db is a class that contains my database access functions - it seemed like a good spot to store a public variable that would need to be accessible from anywhere.
Now the code above obviously works fine. If a contact is selected, great - we go to the edit screen. If not, we go to the find-contact screen. But the problem is what happens once the user has selected a contact? It doesn't take them back to the edit screen again.
My ideal flow would be, in pseudo-code:
editContact()
{
if (contact is selected)
editContact();
else
{
findContact();
// once found, go to edit contact screen
editContact();
}
}
Bearing in mind I am brand new to android development, I was wondering if anyone could give me some pointers on creating the 'flow' of the application, as described above.
If I understand your problem there could be several ways to do it. I would probably simply disable the "Edit" Button if they haven't selected a contact yet because they shouldn't be able to try and edit if nothing is selected. However, if this doesn't work for you then you could pass a flag with your Intent. One thing I do sometimes is to pass a "source" flag.
After clicking edit and no contact is selected
Intent i = new Intent(Edit.this, FindContact.class);
i.putExtra("source", "edit");
startActivity(i);
then in your find contact get the extra
Intent intent = getIntent();
if ("edit".equals(intent.getStringExtra("source");
{
// create intent for EditScreen
You also could simply pass a boolean value this way with key "edit" and check if that is true. If so, then go to EditScreen if not go wherever you need to. I hope this helps. Let me know if I misunderstand.
You also will find different Intent.FLAGS very valuable
Actually android has a philosophy around the "flow" of activities which is covered here.
Since you are a new android developer it's mandatory to understand how activities are working.
By the info in your question it seems that you are not following android guidelines which is not necessarily a bad thing but you must have valid reasons not to. So by the programmatic view of your question I suggest that you disable the edit and delete buttons when a contact is not selected or if you prefer, to restrict user's choices based on the data selected or displayed.
However you are asking about the "flow" so my advice here is to study more about activities and how the back stack is working and look at the sdk examples. In most of the applications that a user can make some actions based on data selected those actions are presented to the user after a longclick on a record. In your case a pseudo flow would be something like this:
Find Activity --> Results --> Click on a Result --> Choices List (Edit, Delete e.t.c) --> Edit or Delete Activity (after edits goes back to Results)
Now about "creating a less linear flow of activities" IMO is a little too generic. You can control the flow of your activities as you want by using either the techniques in the design (look my link on top) or by code by using for example the finish method which removes the activity from the back stack.
Hope this helps...

Android back-button-overriding etiquette / guidelines

I have an app in which the user logs in from a main activity, and then can browse through a heirarchy of entities using listviews. So, the Activity stack would look something like this:
A -> B -> B -> B -> ...
where the number of B's is proportional to how deep you are in the tree of entities.
Frequently, I find myself pressing backbackbackbackback to get to the root (the first 'B'), but one too many presses and I log myself out, or even leave the app. I'm considering overriding the back button so that, when pressed from the root B, it will pop up a dialog essentially saying "Log out? (Y/N)", thus blocking a string of back-presses from completely exiting the app.
I've noticed a sort of sensitivity regarding overriding the back button, though, and - while it makes sense to me - I want to know if this is considered a good use of the back button.
So:
Would this be considered an appropriate/conventional override of the back button?
Is there a better/more conventional way to accomplish this without overriding?
Also, so this question might be more generally useful in the future, are there any guidelines for what is acceptable/unacceptable when overriding the back button?
I would find this use acceptable; I've seen a number of apps that ask for a confirm before exit - if the user wants to exit an app, they usually will press the Home button and let Android handle the finish() if and when it's needed.
I know I've accidentally exited an app by pressing back too many times :(
Dotmister's comment about Handcent is spot on - the back button should feel natural to the user; your use seems to adhere to this, in that a user will cycle back through activities as expected for the most part. As he said though, give it a try and test it.
Coincidentally, I have a similar flow in my app, but I've included a button for the root activity.
No it is not normal to overide the back button because the user ecxpects the back button to function as a... ?? back button.
On the other side if it is really that annoying using the back button in the normal way, than a compremise? will always be better. But make sure the user still has the idea he 'controls' the device according his rules, do not make him look for yours. (dutch way of making a point, sorry).
The only way to find out is getting some people to try your application and see if it's annoying or not.

What is the way to navigate in Android app

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.

Categories

Resources