SoftwareEnthusiast
SoftwareEnthusiast

Reputation: 271

Back button closes minimizes the app but when reopened from tasks it is on the previous activity

I'm going into an activity with a button click, but when I want to go back to the previous activity, the app basically gets minimized (as for windows terms) and when I open the app from the on going tasks, it turns on of the previous activity, why does it minimize on back press;

FileUploadTest class extends Activity {
 .......

 public void onBackPressed(View v){
   Intent dashboard = new Intent(this, DashboardActivity.class);

    // Close all views before launching Dashboard
    dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(dashboard);  
 }

Android Manifest xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidhive"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />
  <uses-permission android:name="android.permission.INTERNET" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".DashboardActivity" 
        android:theme="@style/Theme.CustomizedFullScreen">
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />

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

    <!--  Login Activity -->
    <activity
        android:label="Login Account" 
        android:name=".LoginActivity"
        android:theme="@style/Theme.CustomizedFullScreen"></activity>

    <!--  Register Activity -->
    <activity
        android:label="Register New Account" 
        android:name=".RegisterActivity"
        android:theme="@style/Theme.CustomizedFullScreen"></activity>

    <activity
        android:label="Upload to Account" 
        android:name=".FileUploadTest"
        android:theme="@style/Theme.CustomizedFullScreen"></activity>

    <activity
        android:label="View All Products"
        android:name=".AllProductsActivity"
        android:theme="@style/Theme.CustomizedFullScreen"></activity>

</application>

Upvotes: 1

Views: 1391

Answers (3)

user3125900
user3125900

Reputation:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        Intent dashboard = new Intent(this, DashboardActivity.class);
        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        dashboard.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
      // Close all views before launching Dashboard
        // this.finish();
         startActivity(dashboard);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

This should do the trick for you!

Upvotes: 1

The Heist
The Heist

Reputation: 1442

Add this method to your activity class file:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        onBackPressed();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

This will override the Back Key event.

Upvotes: 1

Manjeet Brar
Manjeet Brar

Reputation: 943

public void onBackPressed(View v){
  Intent dashboard = new Intent(this, DashboardActivity.class);

// Close all views before launching Dashboard
   this.finish();
   startActivity(dashboard);  
}

Upvotes: 0

Related Questions