0

If I login giving password and username and it directs to the next page after that if I click back press key on my device not back button back key on device it should remain in the same page and on double click of back key it should run out of the app.

Can anybody suggest me some codes? ASAP and If I run out of the app it should come to the page where I left. I want the app to get pause or remain on the same page where I closed it.It should not ask for login again until I Logout the app.

ManNi PandiT
  • 76
  • 1
  • 9
  • Why do you want to deviate from the 'standard'? User want to go back when they press back, not be stuck in the same screen. – RvdK Oct 18 '13 at 10:04
  • 1
    What does "if I click back press key on my device not back button back key on device" even mean? – Dan Hulme Oct 18 '13 at 10:04
  • @DanHulme ahha it means when I click on back key in device(mobile) not backbutton of my app. – ManNi PandiT Oct 18 '13 at 10:24
  • @RvdK It should remain on the same page when user click once and should close the app if user clicks twice. – ManNi PandiT Oct 18 '13 at 10:26
  • I want the app to get pause or remain on the same page where I closed it.It should not ask for login again until I Logout the app. – ManNi PandiT Oct 18 '13 at 10:56

5 Answers5

3

Override the onKeyDown method in your activity and look for the back button. Return true so that the event is consumed.

    long backPressedAt = System.currentTimeMillis();
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            final long currentTime = System.currentTimeMillis();
            if (currentTime- backPressedAt < 888)
            {
              backPressCount++;
            }
            else
            {
              backPressCount = 0;
            } 
            backPressedAt = currentTime;
            if(backPressCount == 2)
            {
               return super.onKeyDown(keyCode, event);
            }
            return true;
       }
       return super.onKeyDown(keyCode, event);
   }
RPB
  • 15,438
  • 15
  • 52
  • 79
  • use flag also for double click. – Shivang Trivedi Oct 18 '13 at 10:04
  • i think he doesn't want that, he just wants to block back button. – RPB Oct 18 '13 at 10:05
  • he told "it should remain in the same page and on double click of back key it should run out of the app" – Shivang Trivedi Oct 18 '13 at 10:06
  • Lol, I just read the question header and gave the answer, my BAD. – RPB Oct 18 '13 at 10:09
  • @Rinkalkumar hahaha tsp has got it right help me out Rinkal...! – ManNi PandiT Oct 18 '13 at 10:28
  • @ManNiPandiT Have a look at my updated answer. – RPB Oct 18 '13 at 11:01
  • @ManNiPandiT Please accept the answer if you have got yours. – RPB Oct 18 '13 at 11:02
  • @Rinkalkumar yeah thanks for that but you are not noticing what I asked again . I want the app to get pause or remain on the same page where I closed it.It should not ask for login again until I Logout the app.Is there only shared preference no easy way? – ManNi PandiT Oct 18 '13 at 11:17
  • Yes, you can use shared preference the easiest way or you can use SQLite. – RPB Oct 18 '13 at 11:26
  • @Rinkalkumar yes I am using SQLite how to do it.I have created registration page,login page and that all is working. But I want this " I want the app to get pause or remain on the same page where I closed it.It should not ask for login again until I Logout the app" how to do it .Is there any other way other then Shared Preference ah? – ManNi PandiT Oct 18 '13 at 11:34
2
@Override
public void onBackPressed() {

}
Broak
  • 4,106
  • 4
  • 29
  • 54
1

Try this snippet

@Override
    public void onBackPressed() {
        if (doubleBackToExitPressedOnce) {
            super.onBackPressed();
            return;
        }
        this.doubleBackToExitPressedOnce = true;
        Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
             doubleBackToExitPressedOnce=false;   

            }
        }, 2000);
    } 

Source

Community
  • 1
  • 1
Kalai.G
  • 1,690
  • 13
  • 22
  • Do I have to import doubleBackToExitPressedOnce what is it ?explain showing error on it. – ManNi PandiT Oct 18 '13 at 10:41
  • It's just a boolean in your class that you create. – Carnal Oct 18 '13 at 10:44
  • @Carnel Oh thanks carnel – ManNi PandiT Oct 18 '13 at 10:47
  • @Kalai.G when I click Once it is showing the Toast but when I click Twice it Come back to the Login Page Then whats the use of getting login?I want it to remain on same page on one click and runout after another click. – ManNi PandiT Oct 18 '13 at 11:05
  • @Carnel when I click back Once it is showing the Toast but when I click Twice it Come back to the Login Page Then whats the use of getting login? I want it to remain on same page on one click and runout after another click.And when I come back the page where I left should open...! – ManNi PandiT Oct 18 '13 at 11:06
-1

Override the onBackPressed method on the Button Click. This will solve your problem.

Droid
  • 419
  • 4
  • 15
-2

First add noHistory=true under login activity tag into your manifest

<activity
    android:name="com.example.MainActivity"
    android:label="@string/app_name"
    android:noHistory="true" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

After that add onBackPressed() like below into your next activity

private int count=0;
    @Override
    public void onBackPressed() {
      if(count!=0)
        super.onBackPressed();
      count++;
    }
SathishKumar
  • 1,634
  • 1
  • 13
  • 28
  • Thanks man this look good i will try this out now. – ManNi PandiT Oct 18 '13 at 10:30
  • well it worked .but on double click it is going to login screen again i dont want that i want it to remain on the same page on single click or on doubleclick it should go out of the app. – ManNi PandiT Oct 18 '13 at 10:54
  • @ManNiPandiT Did you add that noHistory into your manifest – SathishKumar Oct 18 '13 at 10:56
  • ya thanks man its working i din do that.Can you help me out on this? i want my app to remain or get pause on same page where I left back. It should not ask for login Until I give logout. – ManNi PandiT Oct 18 '13 at 10:59
  • @ManNiPandiT you need to maintain session to achieve this. Use sharedpreference to store your session. Depends on login session you need to redirect your activity – SathishKumar Oct 18 '13 at 11:07