-1

I am new to android and trying my level best.

I am having an application with three activities, 1 login page 2. Home page 3. Registration Page

The first activity loaded is login when a user gives correct user name and password I am allowing to Home page Here I am facing a problem, when I click back button I am going to the login screen again it should not happen the application has to close.

while creating intent and loading home activity i am using finish() also

I tried onBackPressed() method but it does not work for me

    public void onBackPressed() {
              //      HomeActivity.this.finish();
                      super.onBackPressed();
      }

And my manifest file is

        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.Example.MyProject">
        <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:icon="@drawable/abc"
        android:label="@string/app_name_display"
        android:supportsRtl="true"
        android:largeHeap="true"
       android:manageSpaceActivity=".LoginActivity"
       android:theme="@style/AppTheme">
       <activity android:name=".LoginActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service android:name=".SyncData" >
    </service>
    <activity android:name=".HomeActivity"
        android:screenOrientation="portrait"/>
       <activity android:name=".Registration"  />
       </application>

      </manifest>
Nitesh
  • 137
  • 1
  • 8
  • When you launch `Home` from `Login` do you also call `finish()`? If so, pressing BACK in `Login` should NOT return to `Home`. Add your manifest to your question please – David Wasser Dec 09 '16 at 12:34
  • yes i am calling but after logout also when i press back button it is loading home screen – Nitesh Dec 09 '16 at 12:38
  • That is not standard Android behaviour. You may be seeing this nasty Android bug: http://stackoverflow.com/a/16447508/769265 Please force stop your app and then start it again from the HOME screen. Then login and press the BACK button and see if your `HomeActivity` still shows. – David Wasser Dec 09 '16 at 13:42
  • 1
    @DavidWasser, you are correct, Thank you for your helpful link – Nitesh Dec 10 '16 at 05:48
  • Sorry about that. A lot of people have been bitten by this bug :-( – David Wasser Dec 10 '16 at 06:55

4 Answers4

2
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);

startActivity(intent);
finish();

finish() will destroy your Login activity once you reach to HomeActivity.java and you wont reach to LoginActivity again.

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Sandip Lawate
  • 456
  • 3
  • 11
0

Try this one..

 private Boolean exit = false;

   @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        if (exit) {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            finish();
        } else {
            Toast.makeText(this, "Tap again to exit.", Toast.LENGTH_SHORT)
                    .show();
            exit = true;

    }
}
Rajakumar
  • 907
  • 1
  • 7
  • 17
  • In normal cases its works fine, but in my allocation having some scenarios like, while logout i am asking wheatherhe want to log out (Yes/no) if user clicks yes then logout is processed here when i pressed back agian is is going to home screen – Nitesh Dec 09 '16 at 11:16
0

Just Override your onBackPressed on the second Activity and do not call

super.onBackPressed();

For instance,Override your onBackPressed like this:

@Override
public void onBackPressed() {
//Do something else here, maybe prompt the user if he needs to exit the app.
finishAffinity();;//If you want to close the application.
}

I would add something like this, to be user friendly

@Override
public void onBackPressed() {
android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder(this);
        alertDialogBuilder.setMessage("Do you wish to exit the app?");

        alertDialogBuilder.setPositiveButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int arg1) {
                dialog.dismiss();
            }
        });

        alertDialogBuilder.setNegativeButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                finishAffinity();
            }
        });

        android.support.v7.app.AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();
    }
SWAT
  • 1,107
  • 8
  • 19
  • If we do like this back press button itself will not work – Nitesh Dec 09 '16 at 11:38
  • its not my requirment – Nitesh Dec 09 '16 at 11:39
  • Nitesh, as I understand from the question, the back is not supposed to work. – SWAT Dec 09 '16 at 11:43
  • no no, the back button should not lead us to login activity, consider a example like for facebook if we login once, the application will open and while closing the application it will not lead to login page na – Nitesh Dec 09 '16 at 11:48
  • I am not concern about alert messages and their functionallity, here the major problem is onBackPressed i am going to previous activity that should not happen. For ur clear understanding i will tell you one thing. After Logout when i press back button it is going to launch home activity – Nitesh Dec 09 '16 at 11:56
  • You can use finishAffinity(); – SWAT Dec 09 '16 at 12:12
  • It will close whole application – Nitesh Dec 09 '16 at 12:26
  • Please rephrase the question if you need a valid response. "I am going to the login screen again it should not happen the application has to close." This is what you asked, that is what I replied for. – SWAT Dec 09 '16 at 14:32
0

Another user bitten by this nasty long-standing Android bug.

If I had a dollar for every hour that was spent by software developers trying to figure this out, I wouldn't have to work anymore.

Google: FIX IT!

Community
  • 1
  • 1
David Wasser
  • 93,459
  • 16
  • 209
  • 274