I have two activity.
One of these is a sort of home activity (with some choises) and the other is the main activity.
Use do this:
<activity
android:name=".Home"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
</activity>
And in Home.java i set:
#Override
protected void onPause() {
super.onPause();
finish();
}
#Override
protected void onStop() {
super.onStop();
finish();
}
In MainActivity.java I do nothing in method onPause() and onStop().
The problem is that if in MainActivity I set back, the app is in my background app and I can see the screen of MainActivity.
But when I restart the app, it restart with Home.
Well, how can I restart with MainActivity? Thanks
If you want your application to always launch from MainActivity you need to move the intent-filter to it in the manifest file.
<activity
android:name=".Home"
android:label="#string/app_name"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You are also getting this behavior because you are calling finish() in your onStop(). As described here you are forcing a onDestroy() in your onStop().
Try this:
#Override
protected void onPause() {
super.onPause();
system.exit(0);
}
Related
I recently added a splash-screen to my app and made it as launcher activity to display it when the app started, the manifest looks like,
<activity android:name=".LaucherActivity" android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And I have the MainActivity which is already a launcher activity in the manifest shown below,
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.TransparentTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> s
</intent-filter>
</activity>
So, this is actually creating two APKs, one for the splash and the other main activity. But I wanted the SplashActivity and MainActivity in the same APK running one after the other. How can I achieve that? I found many related questions but none of them are working for me.
Use <intent-filter> only once in manifest. like this
<activity android:name=".LaucherActivity" android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.TransparentTheme">
</activity>
public class LaucherActivity extends Activity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(LaucherActivity.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
Remove this from you MainActivity.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Now call your MainActivity from your Splash Activity
I have created a sample custom launcher to check the behavior of launchMode and intents. Here is the MainActivity.java:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
Here is the AndroidManifest.xml contents:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="nosensor"
android:resumeWhilePausing="true"
android:taskAffinity=""
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
After installing the app when it is launched from app drawer, MainActivity starts with the intent - action as MAIN and category as LAUNCHER. Here it creates one instance. After that in Settings, if it is selected as the default launcher, MainActivity starts with the intent - action as MAIN and category as HOME. Here it creates another instance.
But if it is done in the reverse order, that is first from Settings and then from app drawer, it creates only instance.
So, why two instances are created?
I've added a new Activity, but the Toast message doesn't ever appear. It's not Toast specific though, it doesn't reach this class in general.
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// This should appear
Toast(..., "Inside of SecondActivity onCreate", ...).show();
}
...
My AndroidManifest looks like this,
<application
...
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity" ></activity>
</application>
you never call show() on your Toast
the launcher activity is MainActivity, but the Toast is inside SecondActivity. Did you add the logic to launch SecondActivity?
Edit:
From your Manifest I can read
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
So, when you execute your Application, the first activity launched will be MainActivity. Inside it you need to launch SecondActivity. For instance inside MainActivity's onCreate you could have something like:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
My app is a Launcher.
start an app on launcher.
when my launcher is in background, from logcat I can see that Application is created but the main activity is not created.
public class LauncherApplication extends Application {
#Override
public void onCreate()
{
super.onCreate();
......
Log.d("TAG", "onCreated");
}
}
public class MyLauncher extends Activity {
.....
}
Manifest.xml
<application
android:name="com.launcher.LauncherApplication"
android:label="#string/application_name"
android:hardwareAccelerated="true"
android:process="android.process.acore"
android:largeHeap="true"
android:persistent="true" >
<activity
android:name="com.launcher.MyLauncher"
android:configChanges="mcc|mnc|keyboardHidden|keyboard|orientation|screenLayout|screenSize|smallestScreenSize|navigation|uiMode"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true"
android:screenOrientation="portrait"
android:persistent="true"
android:allowBackup="true"
android:windowSoftInputMode="stateUnspecified|adjustPan" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.MONKEY" />
</intent-filter>
</activity>
......
</Application>
MyLauncher is the main activity.
My question is:
when log prints "onCreated", that means my launcher is killed bo OS in background and LauncherApplication is restarted ?
I have play buttons on custom popups and they are taking me to the wrong activity and im not sure why. The activity its launching is not even in the onclick method.
Button playit = (Button) dialog.findViewById(R.id.playDetourDialog);
playit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
System.out.println("go to pyramid");
Intent i = new Intent(getApplicationContext(), PyramidGamePlay.class);
startActivity(i);
dialog.dismiss();
}
});
manifest:
<?xml version="1.0" encoding="utf-8"?>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Main"
android:label="#string/app_name" android:theme="#android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:theme="#android:style/Theme.NoTitleBar" android:name="GamePlayRegular"></activity>
<activity android:theme="#android:style/Theme.NoTitleBar" android:name="WorldSelect"></activity>
<activity android:theme="#android:style/Theme.NoTitleBar" android:name="OptionsPage"></activity>
<activity android:theme="#android:style/Theme.NoTitleBar" android:name="World"></activity>
<activity android:name="GameEndPage" android:theme="#android:style/Theme.NoTitleBar"></activity>
<activity android:theme="#android:style/Theme.NoTitleBar" android:name="PyramidGamePlay"></activity>
</application>
Use like this
<activity android:name="PyramidGameplay">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
It's hard to know without more information. Have you defined your activity in your AndroidManifest.xml? What info does logcat print when you tap the playit button?
It would also be good to use the android.util.log package for logging, rather than using System.out.println.