Android: Clear the back stack

Try adding FLAG_ACTIVITY_NEW_TASK as described in the docs for FLAG_ACTIVITY_CLEAR_TOP: This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. … Read more

Clear the entire history stack and start a new activity on Android

In API level 11 a new Intent Flag was added just for this: Intent.FLAG_ACTIVITY_CLEAR_TASK Just to clarify, use this: Java intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); Kotlin intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK Unfortunately for API lvl <= 10, I haven’t yet found a clean solution to this. The “DontHackAndroidLikeThis” solution is indeed pure hackery. You should not do … Read more

Finish all previous activities

Use: Intent intent = new Intent(getApplicationContext(), Home.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); This will clear all the activities on top of home. Assuming you are finishing the login screen when the user logs in and home is created and afterward all the screens from 1 to 5 on top of that one. The code I posted will return … Read more

How to restart Activity in Android

I did my theme switcher like this: Intent intent = getIntent(); finish(); startActivity(intent); Basically, I’m calling finish() first, and I’m using the exact same intent this activity was started with. That seems to do the trick? UPDATE: As pointed out by Ralf below, Activity.recreate() is the way to go in API 11 and beyond. This … Read more

OnActivityResult method is deprecated, what is the alternative?

A basic training is available at developer.android.com. Here is an example on how to convert the existing code with the new one: The old way: public void openSomeActivityForResult() { Intent intent = new Intent(this, SomeActivity.class); startActivityForResult(intent, 123); } @Override protected void onActivityResult (int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK && requestCode … Read more

Change application’s starting activity

Yes, you use the AndroidManifest.xml file. You can actually even have more than one launcher activity specified in your application manifest. To make an activity seen on the launcher you add these attributes to your activity in the manifest: <intent-filter> <action android:name=”android.intent.action.MAIN” /> <category android:name=”android.intent.category.LAUNCHER” /> </intent-filter>

getApplication() vs. getApplicationContext()

Very interesting question. I think it’s mainly a semantic meaning, and may also be due to historical reasons. Although in current Android Activity and Service implementations, getApplication() and getApplicationContext() return the same object, there is no guarantee that this will always be the case (for example, in a specific vendor implementation). So if you want … Read more

How to handle screen orientation change when progress dialog and background thread active?

Edit: Google engineers do not recommend this approach, as described by Dianne Hackborn (a.k.a. hackbod) in this StackOverflow post. Check out this blog post for more information. You have to add this to the activity declaration in the manifest: android:configChanges=”orientation|screenSize” so it looks like <activity android:label=”@string/app_name” android:configChanges=”orientation|screenSize|keyboardHidden” android:name=”.your.package”> The matter is that the system destroys … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)