I have an Android app whose MainActivity contains a list of Chapters/Units let's say.
Now when you click on it, each Unit has "N" number of topics as another list.
When you open any one of them it has the topic explained, and at the bottom there are two "Next" and "Previous" buttons that takes you the next or previous TOPIC respectively.
What happens is
1. I open Unit I, then open Topic 1 and continue till I'm at Topic 8 by pressing "Next" button each time.
2. Now when I press the Back/Up Navigation button I'm being taken to the previous Topic (Topic 7), which I don't want because I'll have to back 8 times to get back to the TOPIC LIST.
What I want is to be taken back to the TOPIC LIST directly instead of going back to previous activity.
How do I achieve it?
The very simple solution to your issue is -
on Previous and next click add finish();
something like this -
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.previous:
goToPreviousTopic();
finish();
break;
case R.id.next:
goToNextTopic();
finish();
break;
}
}
So in background your main activity will be present and in activity stack there will be only one activity after the main activity.
I'm thincking about 2 solutions abbout your your problem (but only if your topics are extends to activity):
First, you can override the onBackPressed method from Activity
#Override
public void onBackPressed() {
// Go to TOPICLIST
}
Second, you change the history of activities to disable the way to back on it.
<activity android:name=".activity.topic.subTopic"
android:noHistory="true"></activity>
Related
I am absolute beginner to Android. Now I am creating a tutorial project. In my project, there are so many fragments and there is only one activity for that fragment. So when I press the back the button, application always exit. So it does not make any sense for user.
So what I want to do is, I would like to go back to previous fragment if it exists when user press the back button. So I found so many identical question on stackoverflow. All the answers say to do like this. To go back to previous fragment in Activity.
getFragmentManager().popBackStack();
So this is how I override back button listener in my activity:
#Override
public void onBackPressed(){
if ( getFragmentManager().getBackStackEntryCount() > 0)
{
getFragmentManager().popBackStack();
return;
}
}
But it is not working when I press the back button. Is this correct way to do it?
When you replace / add fragment, did you addToBackStack?
Demo :
getFragmentManager().beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).replace(R.id.app_bar_main_container, YOUR_FRAGMENT,YOUR_FRAGMENT_TAG).addToBackStack(YOUR_FRAGMENT_TAG).commit();
YOUR_FRAGMENT_TAG needs to be unique for each fragment to be able to go back to all previous fragments.
I have Activity A (Main) and Activity B.
Fact: Activity A has: android:launchMode="singleInstance"
Usual scenario is:
User launches application > Activity A.
User clicks an item > Activity B
3.A. If user clicks on back/up buttons > Back to A (without calling finish() on B)
User clicks the SAME item as before > Forth to B.
At this point he can go back and forth without new instances. It's all in the stack and it doesn't recreate activities. (All right!)
3.B. If user clicks Home, then goes to task manager and brings the app to front > Activity B (all good, so far)
If user clicks UP button, it goes to TASK MANAGER, and I want it to go to Activity A (back button is expected to work this way, so let's focus on UP button).
Here's the implementation I have in Activity B for BACK and UP buttons.
#Override
public void onBackPressed() {
moveTaskToBack(true);
// I don't want to finish() the activity, so the user can reclick the same
// item without reloading the whole activity again (webview slow when parsing html).
return;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
moveTaskToBack(true);
// I don't want to finish() the activity... idem.
// I need to implement here the bring Activity A to front
break;
}
}
So, what I want is: to "Go Back" to Activity A keeping the same idea of using the stack to reload Activity B if needed, without using Intents (unless it calls activity to front, without adding items to the stack.)
Let me know if I explained myself clearly and if you need more info.
UPDATE:
I've found this at: http://developer.android.com/training/implementing-navigation/ancestral.html
This is how I adapted it.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent upIntent = new Intent(this, Activity_A.class);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
} else {
moveTaskToBack(true); // I want it this way. Don't worry.
}
break;
}
}
But the method NavUtils.shouldUpRecreateTask is ALWAYS returning false.
I did the http://developer.android.com/training/implementing-navigation/ancestral.html#SpecifyParent part, so that's not the issue.
My problem is that I want to recognize if Activity A exists in the stack, for when i.e. the app is launched from the task manager.
How can I achieve this?
Thanks.
moveTaskToBack moves the entire task to the background. it doesn't finish the activity.
in order to have full control of activities, you have some possible solutions:
create your own global manager for the activities, monitor each of them through all of their lifecycle and decide what to do on each event.
you could also finish each activity as soon as you go from it, and put "it" (just its name or something) in a stack and restore its state when you come back to it.
use fragments instead, and manage them all on a single activity. be warned of configurations changes though.
I am developing an application which starts from LoginPage. When user Login then he moves to Main Screen where grid view for different departments are present.
Every page of application except login page has a Footer which have different Icons like Home, logout, etc.
I want to add conditional back functionality using mobile back button. Some conditions are as follow:
1) LoginPage ---> Main Screen ---> On back user should log out and go to Login Page
2) Main Screen --> any department ---> Any Sub deprtment --> If user press Back button then go to back in same order
3) User is any where in application ---> If press home button from Footer ---> Comes to Main Screen --> No back functioality to go on previous page, It should follow condition 1.
4) If User on Login Page then he will exit from application on pressing Back Button
5) If User on main Screen then user should logout and go to Login Page on preseeing Back Button
I have tried with "noHistory=true" in Manifest and with Intent flags in Activity file.
Can any body suggest me best way to solved out it.
shouldn't be a problem, all you have to do is override the onBack function and add the logout process.
not a problem, the normal behavior of back buttons is exactly that.
DO NOT DO THIS!!! BAD BEHAVIOR.
normal behavior of back button.
that was step one.
this is used for exit from application on back press.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_BACK) {
finish();
System.exit(0);
}
return super.onKeyDown(keyCode, event);
}
if u want only back then remove System.exit(0) from above code .
By using this you can manage your all condition which one you want.
Use a stack globally to save screens order. Stack must be available in application level. And get the screen order when you click on back button. Write switch case for screen order and start that activity. that's it.
for example.
crate a class class MyStack{
//here declare a static stack
//create setter,getter method for assinging values to stack
}
when starting new activity assing screen value in stack with setter method
if you are starting a activity from main screen assign 1 into stack, you are starting sub screen assign 2 into stack.
when click on back get that value
switch(value){
case 1: //start mainscreen break;
case 2: //start sub screen break;
}
With what I understand, you cannot override the functionality of home button. By default, it minimizes your app with its current state, by calling onPause(). When you open the app again, onResume() is called and starts the app from where it was paused. As far as your back button functionality is concerned, most of the above answers are fine.
Try,
#Override
public void onBackPressed()
{
finish(); //finishes the current activity and doesnt save in stock
Intent i = new Intent(CurrentActivity.this, Login.class);
i.addflags(Intent.flag_activity_no_history);
startActivity(i);
}
Try this to trap events on the back button
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK) {
Intent Act2Intent = new Intent(thisActivity, Activity2.class);
startActivity(Act2Intent);
finish();
return true;
}
return false;
}
on each activity implement
OnBackPress().
Override it and add the functionality you want like logging out, clearing history stack and start new(previous) activity.
I think simplest approach may be to override back button in your "Main Screen" activity so that when back button is pressed you can do :
1. Executing log out logic:
2. Explicitly call your Login Page
This may give the behavior you are looking for.
On how to override back button, you can refer to this link:
http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html
Hope this helps!
I have some strange effects in my Android App.
I use a TabHoster with TabGroupActivities for each Tab. Works pretty good except the behaviour of the Back Button.
An Activity is launched and responds to the Back button. Then I start a Child Activity with a ViewSwitcher. When i hit the Back Button on View A Activity is dismissed as Expected. But it gets through the onBackPressed() of my Activity.
When i hit the Back Button on View B (detailView==true) the Method is not even Called. Instead of that the onBackPressed() of the TabGroupActivity is Called and I am not able to switch back to the first View.
#Override
public void onBackPressed() {
if (detailView == true){
vf.showPrevious();
detailView = false;
}
else {
super.onBackPressed();
}
return;
}
Can Anybody explain this and/or tell me how to switch between Views in an Activity in an ActivityGroup?
Few Days later i came to the Conclusion that it is FOR MY CASE the easiest solution to throw away the ViewSwitching stuff and transfer them to seperate Activities.
Sebastian Olsson is surely right with the fragments but it would be more effort for my specific App to rebuild everything to fragments.
Today i met with the serious problem. Actually i have 5 activities in my activity group.
from 1st activity i go to 2nd and from 2nd i go to 3rd and so on....
and on the 5th screen when i press back key i came to 4th and so on....
When i again go to the same process it displays me the previous displayed data as well on the screen. m not able to find the sollution for the same.
i need that every time i follow the process it will show me the new data not with the previous one data.
pls help me.
i cant paste the whole code. its 5 activities.
I don't think you mean "activity group," which is something else entirely. I assume you're talking about the task stack. What you probably want to do is override the onRestart method on your activity, setting it to call whatever method it is that you use to refresh your view's underlying data. You may want to do that in conjunction with a member variable "dirty" boolean flag you set on your activity before you start another one so that it only triggers if you're coming back from another activity (and not when the user switches to another app and back to yours).
You can override back button of Android .
In that method you can clear all views from stack and set first view on screen.
Below is Code from my app this you can use as per your requirement.
#Override
public void onBackPressed() {
super.onBackPressed();
Tab2GroupActivity.group.back();
}
In Tab2GroupActivity class
public void back() {
if (history.size() > 0) {
history.remove(history.size() - 1);
setContentView(history.get(history.size() - 1));
} else {
finish();
}
}
Here history is my list which contains all opened views in it.
this solved my issue.