Back functionality in android - android

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!

Related

Android Back Stack - UP button method

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.

How to pass login screen activity when using onBackPressed

My app has three main activity.
Splash Activity
Login Activity
Menu Activity
When I start app, splash screen work with four second, then it goes login activity. If user login successfully then app goes menu activity. In my menu, there is logout button. If user clicks it, then app goes login activity.It works fine. But if users don't want to logout and click back button on devices, I want to exit from app directly. If I couldn't find solution for this problem, users have to go login screen, then splash screen and finally exit when they want to exit from app with using back button. Which solution should I use?
After the login has been successful you can call finish() on your Login Activity just after you started your Menu Activity. This effectively removes the Login Activity from the back stack.
Edit: As mentioned in the comment below, this also applies to your Splash Activity.
The solution provided by #Keyboardsurfer will work extremely good for you, but if you want to handle Back button event then use below code to get notified about back button event.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "back button pressed");
// your code to handle back button event.
return true;
}
return super.onKeyDown(keyCode, event);
}
When the login is successful you should call your Menu Activity as follows:
Intent intent=new Intent(LoginActivity.this,MenuActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this.finish();
I think you are checking in your Splash Activity if the user is logged via SharedPreferences, you should close your Splash Activity in the same way that you specify when you call any of the two activities.
You have to override the Activity's (Splash only) onPause method:
Here is how;
#Override
protected void onPause() {
super.onPause();
this.finish();
}
and in login activity call the finish method after the login stuff is done. Answer Edited (See the discussion below)
I don't think it would cause unexpected behaviour in other Activities, only the Splash Activity cause its its onPause() that was override not the others, and since splash is only called once i don't think it matters.

How to prevent going back to the previous activity?

When the BACK button is pressed on the phone, I want to prevent a specific activity from returning to its previous one.
Specifically, I have login and sign up screens, both start a new activity called HomeScreen when successful login/signup occurs. Once HomeScreen is started, I want to prevent the users from being able to return to the login or sign up screens by pressing the BACK key.
I tried using Intent.FLAG_ACTIVITY_NO_HISTORY, but since the application has Facebook integration, when the 'Login with Facebook' is used, Facebook should return to the initial login screen, therefore I should keep a history of these activities.
I thought of overriding the behaviour of the BACK button on HomeScreen to directly finish an application when the button is pressed and I used
#Override
public void onBackPressed() {
finish();
}
but that also does not work.
My suggestion would be to finish the activity that you don't want the users to go back to. For instance, in your sign in activity, right after you call startActivity, call finish(). When the users hit the back button, they will not be able to go to the sign in activity because it has been killed off the stack.
Following solution can be pretty useful in the usual login / main activity scenario or implementing a blocking screen.
To minimize the app rather than going back to previous activity, you can override onBackPressed() like this:
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
moveTaskToBack(boolean nonRoot) leaves your back stack as it is, just puts your task (all activities) in background. Same as if user pressed Home button.
Parameter boolean nonRoot - If false then this only works if the activity is the root of a task; if true it will work for any activity in a task.
I'm not sure exactly what you want, but it sounds like it should be possible, and it also sounds like you're already on the right track.
Here are a few links that might help:
Disable back button in android
MyActivity.java =>
#Override
public void onBackPressed() {
return;
}
How can I disable 'go back' to some activity?
AndroidManifest.xml =>
<activity android:name=".SplashActivity" android:noHistory="true"/>
There are two solutions for your case, activity A starts activity B, but you do not want to back to activity A in activity B.
1. Removed previous activity A from back stack.
Intent intent = new Intent(activityA.this, activityB.class);
startActivity(intent);
finish(); // Destroy activity A and not exist in Back stack
2. Disabled go back button action in activity B.
There are two ways to prevent go back event as below,
1) Recommend approach
#Override
public void onBackPressed() {
}
2)Override onKeyDown method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK) {
return false;
}
return super.onKeyDown(keyCode, event);
}
Hope that it is useful, but still depends on your situations.
Since there are already many great solutions suggested, ill try to give a more dipictive explanation.
How to skip going back to the previous activity?
Remove the previous Activity from Backstack. Simple
How to remove the previous Activity from Backstack?
Call finish() method
The Normal Flow:
All the activities are stored in a Stack known as Backstack.
When you start a new Activity(startActivity(...)) then the new Activity is pushed to top of the stack and when you press back button the Activity is popped from the stack.
One key point to note is that when the back button is pressed then finish(); method is called internally. This is the default behavior of onBackPressed() method.
So if you want to skip Activity B?
ie A<--- C
Just add finish(); method after your startActvity(...) in the Activity B
Intent i = new Intent(this, C.class);
startActivity(i);
finish();
finish() gives you method to close current Activity not whole application. And you better don't try to look for methods to kill application. Little advice.
Have you tried conjunction of Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS | Intent.FLAG_ACTIVITY_NO_HISTORY? Remember to use this flags in Intent starting activity!
Put finish() just after
Intent i = new Intent(Summary1.this,MainActivity.class);
startActivity(i);
finish();
If you don't want to go back to all the activities on your application, you can use
android:launchMode="singleTask"
Learn more here: http://developer.android.com/guide/topics/manifest/activity-element.html
paulsm4's answer is the correct one. If in onBackPressed() you just return, it will disable the back button. However, I think a better approach given your use case is to flip the activity logic, i.e. make your home activity the main one, check if the user is signed in there, if not, start the sign in activity. The reason is that if you override the back button in your main activity, most users will be confused when they press back and your app does nothing.
This method is working fine
Intent intent = new Intent(Profile.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
#Override
public void onBackPressed() {
}
When you create onBackPressed() just remove super.onBackPressed();and that should work
Just override the onKeyDown method and check if the back button was pressed.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//Back buttons was pressed, do whatever logic you want
}
return false;
}
Put
finish();
immediately after
ActivityStart
to stop the activity preventing any way of going back to it.
Then add
onCreate(){
getActionBar().setDisplayHomeAsUpEnabled(false);
...
}
to the activity you are starting.
AndroidManifest.xml
<activity
android:name=".welcome.SplashActivity"
android:noHistory="true" // just add this line
android:exported="true">
</activity>

Override Android Back Button

A little info as to why I am attempting to do this: I am using ActivityGroups to open an activity from a tabHost activity and have that new activity stay under the tabs. That part i've got. But when in that new activity, if I use the back button it takes me right out of the tabs activity so I have to click a few times to get back to where I was.
Is there a way to set the back button to go to a specific activity rather than killing the current activity window?
I believe you should be able to do something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// start new Activity here
}
return super.onKeyDown(keyCode, event);
}
But overriding the expected functionality of the back button is not advisable.
In general, I would advise against that because it breaks the UX. The user expects the back button to kill the entire window, especially since you are using the tabhost. To the user, the entire bunch (tabs and all) is a single activity that he wants to exit when he hits the back button.
If you still want to do it, refer to #onBackPressed(). It is called when the activity has detected the user's press of the back key. The default is to finish the activity, but you can make it do whatever you want. I advise care and caution.
You might find some inspiration from here.

activity and back button

on the ui i am having a back button displayed. there are three activities say A,B,C. Activity A is splash screen. Activity B is home screen. Activity C is edit screen. on edit screen only i am having a back button. when the user press that i open the activity B which also contains some sort of http request and response. i want that the previous activity from stack should get displayed? how can i achieve that? i am able to open previous activity on pressing the back button of device?
when i press back button on device there doesnt goes any http request? i want to achieve this behaviour when i press the ui back button.
I think in your back button you may call the intent for activity B and your http request and response code is in onCreate function
But the back button on device will not call onCreate
There is Two solution for this
One as Macarse say, listen onKeyDown
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Intent i = new Intent(ActivityC.this,ActivityB.class);
startActivity(i);
return true;
}
return super.onKeyDown(keyCode, event);
}
And the second method is to write you code on onStart of ActivityB
protected void onStart() {
//http request and response code
}
This onStart will call all the time this when ActivityB open
Hope this help you
First of all, it's not good to have a back button displayed in the ui. Every android device has a back button in it.
If you want to handle the back button in a different way, check this link.
The solution with onKeyDown might work but using onBackPressed is much easier in my opinion.
You can intercept the Back-key with following override:
#Override
public void onBackPressed()
{
//logic here, for example an intent
Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);
}
And for the other way around super.onBackPressed(); returns to previous Activity in history.

Categories

Resources