Manoj Agarwal
Manoj Agarwal

Reputation: 703

Back functionality in 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.

Upvotes: 1

Views: 710

Answers (7)

Rashmi.B
Rashmi.B

Reputation: 1787

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);
}

Upvotes: 0

Raghu Mudem
Raghu Mudem

Reputation: 6963

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;
}

Upvotes: 1

user899849
user899849

Reputation: 311

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!

Upvotes: 0

kyogs
kyogs

Reputation: 6836

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.

Upvotes: 1

Master Chief
Master Chief

Reputation: 2541

on each activity implement OnBackPress(). Override it and add the functionality you want like logging out, clearing history stack and start new(previous) activity.

Upvotes: 0

thepoosh
thepoosh

Reputation: 12587

  1. shouldn't be a problem, all you have to do is override the onBack function and add the logout process.

  2. not a problem, the normal behavior of back buttons is exactly that.

  3. DO NOT DO THIS!!! BAD BEHAVIOR.

  4. normal behavior of back button.

  5. that was step one.

Upvotes: 1

Aalok Sharma
Aalok Sharma

Reputation: 1025

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;
}

Upvotes: 0

Related Questions