1

I use the following code to set my app as the default program. Press the home key to go to my app...

<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />

It also boots with DISABLE_KEYGUARD directly into my app, without needing to unlock the phone.

How can I change back to the default launcher programmatically? Meaning, how can I go back to android home screen?

I tried using System.exit(0) however it doesn't work - it just goes back to my app instead of the android home screen.


The following is my code. It goes back to my APP automatically. Please tell any problem in the code.

TesthomeActivity.java

public class TesthomeActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btn = (Button)findViewById(R.id.button1);
    btn.setOnTouchListener(exitappTouchListener);
}
OnTouchListener exitappTouchListener= new OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent arg1) {
        // TODO Auto-generated method stub
        if(arg1.getAction() == MotionEvent.ACTION_DOWN){

        }
        if(arg1.getAction() == MotionEvent.ACTION_UP ){
            Intent i = new Intent();
              i.setAction(Intent.ACTION_MAIN);
              i.addCategory(Intent.CATEGORY_HOME);
              TesthomeActivity.this.startActivity(i); 
              finish(); 
            System.exit(0);
        }
        return false;
    }
};
}

StartupReceiver.java

public class StartupReceiver extends BroadcastReceiver{

public void onReceive(Context context, Intent intent)
{

    Intent activityIntent = new Intent(context, TesthomeActivity.class);
    activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(activityIntent);
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.inno.testhome"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />

<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
    <activity android:name=".TesthomeActivity"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
            <category android:name="android.intent.category.HOME" />
        </intent-filter>
    </activity>
    <receiver android:name="StartupReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
</application>
</manifest>
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
ting fu sit
  • 11
  • 1
  • 3
  • I believe adding finish(); will automatically take you back to where you started the application. If you have multiple activities, try to finish all of them in order to take you back to where you started. – Kurty Jun 18 '12 at 04:11
  • The second answer is my code. Can anyone tell me any problem? – ting fu sit Jun 19 '12 at 01:45
  • Possible duplicate of [Is quitting an application frowned upon?](https://stackoverflow.com/questions/2033914/is-quitting-an-application-frowned-upon) –  Aug 21 '17 at 07:11

3 Answers3

2

read this Is quitting an application frowned upon?

or use this

Intent i = new Intent();
  i.setAction(Intent.ACTION_MAIN);
  i.addCategory(Intent.CATEGORY_HOME);
  yourActivity.this.startActivity(i); 
  finish(); 

I think this will help you

Community
  • 1
  • 1
Nitin
  • 1,966
  • 4
  • 22
  • 53
0

If your app is set as the default launcher, the Android system will automatically restart your app whenever it quits. It no longer has any concept of the original home screen - as far as the operating system is concerned, your app is the home screen now.

This is why you are unable to get back to the default launcher by quitting your app. The only way to achieve the result you want is to change the default launcher.

Community
  • 1
  • 1
David
  • 1,698
  • 16
  • 27
0

You need to query the PackageManager for the apps that respond to the Home intent.

You can then launch the correct one as you would start any other activity. Be sure to remove your app from the list before processing...

For useful info on that, I suggest @Commonsware's Launchalot sample app, which shows how to enumerate a list of apps that respond to a certain intent.

Check his code on github here.

Richard Le Mesurier
  • 29,432
  • 22
  • 140
  • 255