There are two activities ActivityA and ActivityB, both are made singleTask. Here ActivityB is of category HOME and it is set to always. I am starting ActivityA from a BroadcastReceiver on ACTION_BOOT_COMPLETED, it is starting ActivityA as expected but when HOME KEY is pressed (which is ActivityB), ActivityA is getting destroyed.
What can be the possible reason of it? How can I stop ActivityA from being destroyed? By keeping both activities singleTask.
This is the BroadcastReciever:
public class MyStartupIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED == intent.getAction()) {
Intent i = new Intent(context, ActivityA.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
}
}
And manifest file is as follows:
<activity
android:name="ActivityB"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="ActivityA"
android:label="#string/app_name"
android:launchMode="singleTask"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
</activity>
Related
I'm trying to test that calling a simple method in my Android app will start another activity. getNextStartedActivity always returns the launcher activity.
Here is the test:
#Test
public void clickingButton_shouldStartNextActivity() throws Exception {
CurrentActivity activity = Robolectric.setupActivity(CurrentActivity.class);
activity.startNextActivity();
Intent expectedIntent = new Intent(activity, NextActivity.class);
assertEquals(expectedIntent, Shadows.shadowOf(activity).getNextStartedActivity());
}
And here is the startNextActivity method in CurrentActivity:
public void startNextActivity() {
startActivity(new Intent(this, NextActivity.class));
}
When I run the test, I get LoginActivity (launcher activity) as the actual result. Here LoginActivity in AndroidManifest.xml:
<activity
android:name=".LoginActivity"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
i write launcher activity that have option to show or hide wallpaper to do that i use two theme
android:Theme.Holo.Light.NoActionBar
and
android:Theme.Wallpaper.NoTitleBar
to change it i do
#Override
protected void onNewIntent(final Intent intent) {
super.onNewIntent(intent);
Utils.PrintInfo("MainActivity.onNewIntent");
if (AppSettings.Data.WallpaperThemeChanged) {
AppSettings.Data.WallpaperThemeChanged = false;
startActivity(new Intent(this, ThemeReloadActivity.class));
finish();
return;
}
}
with ThemeReloadActivity like this
public class ThemeReloadActivity extends Activity {
#Override
protected void onResume() {
Utils.PrintError("ThemeReloadActivity.onCreate");
Activity activity = MainActivity.getMainActivity();
activity.finish();
startActivity(new Intent(activity, activity.getClass()));
super.onResume();
}
}
and this is my Manifest fragment for that activity
<activity
android:name="com.maxcom.launcher.MainActivity"
android:clearTaskOnLaunch="true"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:stateNotNeeded="true" >
<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" />
</intent-filter>
</activity>
but sometimes if i spam home theme does not change, it looks like app does not restart at all
You can restart your application by the following code
Intent intent = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
// get rid of this activity instance and start a new one
finish();
Intent intent = new Intent(MyApp.getInstance(), MyActivity.class);
startActivity(intent);
If you want to only restart activity you can do this
this.recreate();
this will recreate your activity.
I have a problem with Main Activity and Splash Screen.There is intent filter both of them
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
When I remove intent-filter from MainActivity the app cannot open.What must I do ?
AndroidManifest.xml
<activity
android:name="com.example.SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.MainActivity"
android:label="#string/app_name" >
</activity>
SplashScreen Class
public class SplashScreen 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(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}
Main Activity Class
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
You need to have intent filter only for your splash screen. And you need to start main activity from your splash screen activity.
Manifest
<activity android:name="com.example.SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.MainActivity"></activity>
Start main activity from splash screen and finish it
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
I have a launcher activity called MainActivity.
MainActivity will start KeyGuardService, and KeyGuardService regist a ScreenOFF receiver, and then in onReceive will start LockScreenActivity use startActivity.
But everytime, MainActivity will be showed before LockScreenActivity. I have tried many solutions according to other Qustions-issue.
Menifest is as fllow:
<activity
android:name=".activities.MainActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:taskAffinity="com.example.mylockscreen.main"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".services.KeyGuardService"
android:label="#string/app_name"/>
<activity
android:name=".activities.LockScreenActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:taskAffinity="com.example.mylockscreen.lock"
android:theme="#android:style/Theme.Translucent"/>
onReceive as follows
BroadcastReceiver mMasterResetReciever = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
Log.d(TAG, Constants.TAG + "service receive " + action);
if ((action.equals("android.intent.action.SCREEN_OFF"))
&& (Preferences.getUserPrefBoolean(context, "app_on", true)))
{
Intent localIntent = new Intent(context, LockScreenActivity.class);
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
localIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
localIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
KeyGuardService.this.startActivity(localIntent);
Log.d(TAG, Constants.TAG + "service start activity");
}
}
};
can anyone tell me why, thanks.
How can I start an Activity without using an Intent? The only rule I have got is
if( var == true ) startActivity();
but startActivity(); needs an Intent as a parameter.
Just create a new intent for the activity you want to start. depending on where you are you will need the app context thought.
Intent i = new Intent(getApplicationContext(), YourActivity.class);
startActivity(i);
Here's how to navigate to a second Activity (another page) using an Intent.
public void onClick(View v)
{
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
Also, don't forget to adjust the AndroidManifest.xml for each Activity.
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<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"
android:label="#string/second_label">
<intent-filter>
<action android:name="android.intent.action.SECOND" /> //should be namespace of your company I guess
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>