Restart singleTask Activity - android

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.

Related

Robolectric getNextStartedActivity always returns launcher activity

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>

Why other activity is destroying when 'Home' key is pressed.?

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>

Contact Picker intent returns to the wrong activity

I'm calling a Contact Picker from an activity named AddFriendActivity. Once a contact is picked, it always returns to the MainActivity rather than the calling activity.
I have checked the android manifest and I can't see any tags that would cause it to prefer MainActivity on resumption. I've extracted (what I think is) the relevant code below.
AndroidManifest.xml
<activity
android:name="x.MainActivity"
android:label="#string/activity_main_title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="x.start.StartActivity"
android:label="#string/start_activity_title"
android:noHistory="true" >
</activity>
<activity
android:name="x.policies.PoliciesActivity"
android:label="#string/activity_policies_title" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="x" />
<activity
android:name="x.AddFriendActivity"
android:label="#string/title_activity_add_friend"
android:parentActivityName="x.friendster.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="MainActivity" />
</activity>
Code for starting the contact picker from AddFriendActivity
btnContacts.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent contactpicker = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(contactpicker, 1);
Log.e("contactpicker", "meh start");
}
});
Not sure if this is relevant, but here's the code that calls the AddFriendActivity from a fragment ChatPanel in MainActivity
btnAddFriend.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent intent = new Intent(getActivity(), AddFriendActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
});
Any idea why this is happening? I've probably missed something simple...
remove line intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
from your OnClickListener and see if it works.
Cheers

Main Activity and Splash Screen Priority Prblm

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();

Start an Activity not from an Intent

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>

Categories

Resources