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

4 Answers4

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
  • 16,006
  • 16
  • 55
  • 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,161
  • 4
  • 31
  • 54
  • Maybe some explanation would be helpfull... – RvdK Oct 18 '13 at 10:03
  • So would proper wording in the question, documented SDK code should not require a whole deal of explanation! – Broak Oct 18 '13 at 10:05
  • No problem, just remember to mark the answer that works for you as correct with the check mark. – Broak Oct 18 '13 at 10:58
  • 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.It should come to the page where I left or went back on my device..! got me? – ManNi PandiT Oct 18 '13 at 11:02
  • @ManNiPandiT is this what you wanted? – RPB Oct 18 '13 at 11:05
  • @RinalKumar Not exactly but now I want this too to happen.Could you please help out. – ManNi PandiT Oct 18 '13 at 11:09
-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,644
  • 1
  • 14
  • 28
  • 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