app icon on click event from home screen - android

Each time my app icon from home screen is clicked, I want the app to start my FirstActivity.java. The problem now is, say I have navigated through my app and is in the ThirdActivity.java and I have clicked the home Button and go to check out my other apps. Again when I click the app icon in home screen, my Application starts from ThirdActivity.java. I want it to start from FirstActivity.java.
I have used clearTaskOnLaunch="true" in manifest for my FirstActivity.java and also checked using android:finishOnTaskLaunch ="true" in all my other activities but the problem is still there. How can I solve this?

All I have to do was declare android:launchMode="singleTask" in manifest for my FirstActivity.java "only". By doing this, every time user selects the app by clicking the app icon in the home screen, app will start with FirstActivity.java
Say the user has navigated through my app and is in the ThirdActivity.java and clicked the home Button. The user has the option of selecting the app again from the background apps section (task manager section). If he/she does so, it will start from where he/she has left (ThirdActivity.java). Only if they click the app icon will the app start from FirstActivity.java

you need to finish you current activity at the time when you are launching intent for new activity, there is a method to do that finish();

In your oncreate you just have to start your activity using Intent
In manifest you have to provide name attribute to application tag
<application
android:allowBackup="true"
android:icon="#drawable/app_logo"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name=".class name which extends Application"
>
Here you can see details on how to use application class:
http://www.intertech.com/Blog/androids-application-class/
http://developer.android.com/reference/android/app/Application.html
http://androidexample.com/Global_Variable_Or_Application_Context_Variable_-_Android_Example/index.php?view=article_discription&aid=114&aaid=136

You can use this code in onPause() of the second third activity but you have to take care of if onPause() is called because of clicking home button or by any other mean, Code is:
Intent intent = new Intent(AnyActivity.this,FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();

You can use moveTaskToBack(true) when you click on the homebutton and start the activity you want to.

I had resolved the issue by adding all three tags android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:finishOnTaskLaunch ="true"
in manifest splash screen activity may be helped some one:
<activity
android:name=".SplashscreenActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:finishOnTaskLaunch ="true"
android:screenOrientation="portrait"
android:theme="#style/AppThemeForAppCompact">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Related

Launcher development - Home button not going back to initial activity

I have an app that acts as a Launcher. This app has 3 activities:
SplashActivity: shows a splash screen while loading, then launches LauncherActivity and finishes. This is the Activity marked as launcher in the manifest.
startActivity(Intent(this, LauncherActivity::class.java))
finish()
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:theme="#style/SplashTheme">
<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>
LauncherActivity: main activity for Launcher. Has a menu button that launches DashboardActivity.
startActivity(Intent(this#LauncherActivity, DashboardActivity::class.java))
<activity
android:name=".LauncherActivity"
android:launchMode="singleTask"
android:screenOrientation="landscape" />
DashboardActivity: shows a list of apps and launches them through their launch intent.
private val DEFAULT_FLAGS_APP_LAUNCH = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(packageManager.getLaunchIntentForPackage(packageInfo.packageName).apply {
flags = DEFAULT_FLAGS_APP_LAUNCH
})
<activity
android:label="#string/apps"
android:theme="#style/TNA"
android:name=".DashboardActivity"
android:launchMode="singleTask"
android:screenOrientation="landscape" />
All activities are launched through startActivity, including the apps.
I want the standard Android Launcher behavior, that is: when entering an app through DashboardActivity, if I click home button, go to the main Launcher activity (LauncherActivity), and when clicking back, go to the dashboard (DashboardActivity).
The problem I have is that when clicking home, it goes back to DashboardActivity, not to LauncherActivity. If I finish DashboardActivity, then when clicking back on an app, it goes back to LauncherActivity.
Any ideas on how to solve this?
This is definitely back/task stack related. See this link for more information about the task stack.
When you go from LauncherActivity to DashboardActivity, the dashboard is placed on to the task stack. When the LauncherActivity is requested again via the HOME button, the task stack is restored back to the last Activity which was in use after launching the LauncherActivity, which was DashboardActivity.
You have several different options to resolve this:
Don't use a separate Activity for the "dashboard". Consider a drawer or even a Fragment which shows the content and can be popped back to the main LauncherActivity when it is done calling startActivity to launch another app.
After your DashboardActivity calls startActivity, it should call finish() so it will get popped off the current task stack.
Usually launchers are setup to be launched in singleInstance mode, preventing multiple instances of the launcher Activity to run at the same time. Note that you'll need to support onNewIntent in your LauncherActivity.
To prevent odd interactions with the task manager, consider setting FLAG_ACTIVITY_NO_HISTORY when launcher your DashboardActivity.

Clear the Backstack within an activity without using FLAG

I want to clear the back stack within an Activity but not by startActivity() and using FLAG. e.g. when I launch an application from the application icon then the application main activity starts but there is some thing in the back stack like the launcher activity because when we touch the minimized app tab the launcher is visible . I want to remove the launcher application from the minimized applications.
#SorryForMyEnglish's answer is right. You just cannot to implement it. By using android:noHistory="boolean" attribute, see my concept maps below:
Because of ActivityC and ActivityD (last activities) has a true value, so they cannot back to MainActivity, but they can back to ActivityA and ActivityB. Also, ActivityA and ActivityB can back to MainActivity. And the backstack is completely cleared without using this startActivity(intent) to open your next Activity (so you will need FLAG):
Intent intent = new Intent(CurrentActivity.this, NextActivityToBeOpened.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
After you declared the value in manifest, you just need to call this startActivity(intent) to open the next Activity (no FLAG is needed):
startActivity(new Intent(CurrentActivity.this, NextActivityToBeOpened.class));
Is it very simple, right?
Remember:
Set your last Activity with android:noHistory="true" in manifest.
Another Activity must be set with false value.
Apply this attribute for all of your Activity.
As additional, here is how to use it inside your manifest:
<activity android:name=".MyActivity" android:noHistory="true" />
use android:noHistory="true" attribute in Manifest for your Activity
<activity android:name="StartActivity"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

How to always open current activity on icon press , after pressing the home key of device

I really having stuff with the Activity launch after press on home key.Suppose i have three A, B, C activity and i disable the back press on device. Suppose A is my main Launcher Activity and i move from A to B and B to C and pressed the home key and again click on icon then it always start A that is the launcher. But i did not want like that when press home key on C then click on icon should always start with C Activity. If i press home key on B Activity then always want to open B actvity on click of icon. How to make this.
And one more thing i do not understand about the at the time installation complete it have two option DONE and OPEN. So when press on Done it work fine on keypress with home with the current Activity but when start with OPEN then it always start the A Activity that launcher one on click on icon after press home key at any Current Actvity.
How to resolve this? thanks guys
Manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sunil.apiv2map"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.sendmyposition.A"
android:configChanges="orientation|keyboardHidden|screenSize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.sendmyposition.B"
android:configChanges="orientation|keyboardHidden|screenSize" >
</activity>
<activity
android:name="com.example.sendmyposition.C"
android:configChanges="orientation|keyboardHidden|screenSize" >
</activity>
</application>
</manifest>
call actvity A to B
Intent intent = new Intent(A.this, B.class)
startActivity(intent);
finish()
and calling B to C
Intent intent = new Intent(B.this, C.class)
startActivity(intent);
You can use Preferences to do this... Package
android.preference provides classes that manage application preferences and implement the preferences UI. Using these ensures that all the preferences within each application are maintained in the same manner and the user experience is consistent with that of the system and other applications.
You can save current Activity name (String) in SharedPreferences, and than read this String in MainActivity after application launch to open last opened activity.
Look jukas answer here: How to start another Activity as main and launcher after Deploying the application into device
Or You can use this PoC from here: How to return to the latest launched activity when re-launching application after pressing HOME?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
// Activity was brought to front and not created,
// Thus finishing this will get us to the last viewed activity
finish();
return;
}
// Regular activity creation code...
}

I click HOME button and the program minimizes; but when I press the app icon, the program is launched twice

I have the following problem:
When I press the Android HOME key, I can see the "Desktop" and my app icon. Then I press my app icon and my application launches twice. I don't want open my app twice.
How my program works:
I have 4 Activities (A, B, C, D).
A - The Main Activity: It is the first to open. It opens the other activity that has a lot of buttons. It's like a Java's main() method. I show a SplashScreen and I call another Activity. Then I finish my activity "A".
B - The Menu Screen: In this activity, I have some buttons, like a menu. I have a configuration button, update button, and Login Button. When I click the login button, I finish this activity and open the Login Screen (Activity "C").
C - The Login Screen: The user writes the Login and Password. If the login is successful, I finish this activity and open the Activity "D".
D - The application main screen: It stays opened all the time and launches another Activities. I finish this when I want close my application.
P.S.: I tried change the launchMode flag (androidManifest.xml), but didn't work.
My AndroidManifest.xml bellow:
<application android:label="#string/app_name" android:icon="#drawable/icon" android:name="MyApplication">
<activity android:name="A"
android:label="#string/app_name"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="br.com.site.B" android:label="#string/app_name" />
<activity android:name="br.com.site.C" android:label="#string/app_name" />
<activity android:name="br.com.site.D" android:label="#string/app_name" />
</application>
And this is my Activity "A.java" source:
public class A extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finish();
startActivity(new Intent(this, AtualizaDadosFrame.class));
}
}
I don't want open my app twice.
Thanks in advance!
I'm going to assume that you started the app initially (the first time) from an IDE (like Eclipse or IntelliJ). If so, this is a known bug in Android (see http://code.google.com/p/android/issues/detail?id=26658 ). Many people have struggled for days chasing this problem :-(
Please don't change the launchMode. This is not the correct way to solve this problem. The default (standard) launchMode is the one that works in most cases.
EDIT (Added link to workaround):
A simple workaround for this problem can be found at http://code.google.com/p/android/issues/detail?id=2373#c21
You should set the desired launch mode in your AndroidManifest.xml.
You can restrict this.....
Please go through below link.
Home key press behaviour

Problem while displaying activity from BroadcastReceiver

Hi I have a Broadcast receiver with following code.
1. Intent i = new Intent(context, A.class);
2. i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
3. context.startActivity(i);
This works fine and my activity does start, but it starts on top of my main activity i.e. B. What I want is that from my broadcast receiver I should be able start an Activity A, such that it does not start on top of B. Why B is always starting in background. What I am doing wrong.
Also, to mention by activity A has Theme.Dialog.
Please let me know your thoughts.
A new activity is supposed to start on top of existing activities.
If you want to start something without appearing in the activity history stack, maybe this something shouldn't be an Activity but a Service instead?
Edit: I think you can change this behaviour with FLAG_ACTIVITY_NEW_TASK in conjunction with android:launchMode="singleTask" in the Manifest. (see documentation on launch mode)
If you start an Activity then of course it will come foreground.The activity previously was in foreground will go to the background automatically.
If you don't need to start it on top of B,what about to start it from service?.Let Activity A stay as it is.
If you need to access anything from service and no need to show the activity,then surely you are looking for other then starting an Activity with an intent.
Finally if you really want to start Activity A,and B will be on top then start Activity B after starting A
I think you mean you want to start the application with a different activity. You can edit your AndroidManifest.xml file to make a different activity start first.
EDIT: Heres an example:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainActivity" <-this will start first
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 =".otherActivity"
android:theme="#android:style/Theme.Dialog">
</activity>
<activity android:name=".anotherActivity"></activity>
</application>

Categories

Resources