Android Run application from last Activity

I have an application with several Activities. My A Activity has the Manifest Intent filter parameters: action.MAIN and category.LAUNCHER. after its being loaded I call Activity B and finish() A since I don't use it anymore.

After I run my application, go from Activity A to B and press the Home button, when I relaunch it from the applications menu or from the Market app for ex.(not by a long press on the Home button), starts from the A Activity and do not save its last Activity B.

I definitely know that this is possible to relaunch an application from its last Activity (some applications from the Market do support it) and I think that this can be determined by the Manifest parameters but I don't know which one.

does anyone know how to implement it so my application can relaunch from its last Activity B?

Thanks ayanir


Asked by: Miller691 | Posted: 20-01-2022






Answer 1

Though I know this is an old question, I was struggling with this very issue and couldn't find an answer on SO. So, here is my (very newbie) answer:

No, I do not think it's possible to do this by messing with the manifest - you can only launch one fixed activity per app from the home screen. What you can do, though, is launch whatever activity you want from that starting point, and Android can do it quickly enough that you never see the first one.

Though this feels very much like a hack, I implemented this routing in the starting activity's onResume() method, and used sharedPreferences to keep track of which activity to launch:

final Class<? extends Activity> activityClass;
        SharedPreferences prefs = getSharedPreferences("sharedPrefs", MODE_PRIVATE);
        int activityID = prefs.getInt("whichActivity", -1);
        if (activityID  == Constants.ACTIVITY_ID_MAINSCREEN) {
            activityClass = MainScreen.class;
        } else {
            activityClass = null; return;
        }
        Intent newActivity = new Intent(this, activityClass);
        this.startActivity(newActivity);

Answered by: Daisy132 | Posted: 21-02-2022



Answer 2

There have been a number of very similar questions lately. It's a good idea to search the site first to ensure that duplicate questions don't get asked.

For example, the question linked below says that this behaviour was happening because the developer was starting their application using the Eclipse debugger. Another person was having this problem because they were launching the application directly from Eclipse, rather than starting cleanly by manually pressing the launcher icon.

Android: keep task's activity stack after restart from HOME

Answered by: Carina120 | Posted: 21-02-2022



Answer 3

so there are a few things to consider when developing Apps in Android. And one of the big things is the Application Lifecyle, if you haven't yet then I would suggest this video. What happens is that an application can be killed and reset at any point in time and you need to save the state of your application so that you can restore it at any time. If you open your App from the Launcher you will always go into the Activity that starts the app, if you want to skip to the next Activity you need to store that information and then jump to the Activity in your code.
Also have a look at this documentation about SavingPersistentState

Answered by: John157 | Posted: 21-02-2022



Similar questions

android - How to clear stack back to root activity when user leaves application?

I have an application with 2 activities, LogonAct and MainAct. LogonAct is a logon activity which I want to force the user to go through each time they return to the application. I've set android:clearTaskOnLaunch="true" on LogonAct. When I first start the app I go through this sequence of screens, Home -&gt; LogonAct -&gt; MainAct -&gt; Home I then follow this sequence...


android - Force application to restart on first activity

For an unknown reason, I can't get my application leaving properly so that when I push the home button and the app icon again, I resume where I was in the app. I would like to force the application to restart on the first Activity. I suppose this has something to do with onDestroy() or maybe onPause() but I don't know what to do.


android - what to know in order running Activity in extern application

I wonder if it's possbile, and if it is, what i`am suppose to know, in order to run an Activity in some application, while i am calling it from another application. for example, now i am on Application A, and i want to run an Activity on Application B. what parameters do i need to know in order to do that? Thanks,


How to limit the number of the same Activity on the stack for an Android application

Is this possible in an Android app? I want to make it so that no matter how many times a user starts activityA, when they hit the back button they will never get more than one occurence of activityA. What I am finding in my current code is that I have only two options: 1. I can call finish() in activityA which will prevent it from being accessible via the back button completely, or 2. I do not call finish(), and th...


android - How to clear stack back to root activity when user leaves application?

I have an application with 2 activities, LogonAct and MainAct. LogonAct is a logon activity which I want to force the user to go through each time they return to the application. I've set android:clearTaskOnLaunch="true" on LogonAct. When I first start the app I go through this sequence of screens, Home -&gt; LogonAct -&gt; MainAct -&gt; Home I then follow this sequence...


android - Force application to restart on first activity

For an unknown reason, I can't get my application leaving properly so that when I push the home button and the app icon again, I resume where I was in the app. I would like to force the application to restart on the first Activity. I suppose this has something to do with onDestroy() or maybe onPause() but I don't know what to do.


android - what to know in order running Activity in extern application

I wonder if it's possbile, and if it is, what i`am suppose to know, in order to run an Activity in some application, while i am calling it from another application. for example, now i am on Application A, and i want to run an Activity on Application B. what parameters do i need to know in order to do that? Thanks,


How to limit the number of the same Activity on the stack for an Android application

Is this possible in an Android app? I want to make it so that no matter how many times a user starts activityA, when they hit the back button they will never get more than one occurence of activityA. What I am finding in my current code is that I have only two options: 1. I can call finish() in activityA which will prevent it from being accessible via the back button completely, or 2. I do not call finish(), and th...


android - Remote service start activity from different application (apk)


android - Launch an activity only the first time the application starts

I need a way to detect if this is the first time the user is ever opening the application, if so, start an activity. Then all previous application launches wouldn't start that activity. I've read in a few places about using preferences to accomplish this. Anyone got any ideas?


Can I have an android activity run only on the first time an application is opened?

OK, so I'm playing around with an android app. The 90% use case is that users want to go straight to the primary list screen to find what they're looking for. That's what I want as my default screen. The first time a user loads the app however, some configuration is required before their list screen is of any value to them. So my question, is how I can go about displaying the configuration activity...


How to log each application activity in android?

Hello I want to log application activity information on my android phone, I just need 2 things, the start time of the application and the end time of the application. I just want to know for how much time a particular application/activity was running. Say for example a user launched music player and after some time closed it? I just want to know how long the music player opened, or if he is talking on phon...


java - Access Application class from class other then Activity

In my application I need data which is accessible for a few activities. I've read that a good solution is to use Application class for this. So I use it like this: public class MyApplication extends Application { private String str; public String getStr(){ return str; } public void setStr(String s){ str = s; } } and I can access this variable from activity ...


android - create one activity application

I want to create an activity that uses the intent action for Screen_off - is there an easy way to do this? I keep getting lost as to where I should start - does the screen_off activity need to be defined somewhere and then referred to?


android application memory leaks

I am using kind of results search engine, problem is to remember the searching criteria i made singleton static class which keeps only one instance. In my application there are lots of class level private variables, lots of public static variables, a big util class which contains only static final methods. problem is my application get crash any time any where any spot and interesting thing is crash code a...


Android Application data should not be released by android OS

public class MYApplication extends Application { String property; setter getter } does above code make sure property will not be collectied by android OS if not used for a long period of time.


Android Email application - Is it possible to get all the details which are configured in Email application

Is there any way to get in the code: Email id Password SMTP host SMTP port which the user used to setup his Email account with the Android built in Email application Required because, I am written a MailSender class using JavaMail API there I need to send the mail using the details what user configured in Built in Android Email application.


Android Intent for Twitter application

Is it possible to show a list of applications (with intent.createChooser) that only show me my twitter apps on my phone (so htc peep (htc hero) or twitdroid). I have tried it with intent.settype("application/twitter") but it doesnt find any apps for twitter and only shows my mail apps. Thank you, Wouter


webserver - Retrieve data from a web server for android application

I am developing an application that needs to retrieve some data from a web server. But I have no idea how can this be possible?


Should a legacy Android application be rebuilt using SDK 2.1?

I have an Android application that uses the well known Strategies for Legacy Applications. It is build with the Android SDK 2.0 with manifest settings minSdkVersion="3" (API 1.5) and targetSdkVersion="5" (2.0). Question1: Since maxSdkVersion is not specified, the application should be...


android - Intent to pick an installed application slow in my activity, why?

If you long press on your homescreen and pick to add an application shortcut, you will be greeted with a listview showing all of your instal applications. I needed this same functionality in my application so I copied the intent from the launcher source: Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); Intent pic...


web services - How to store data accessible to both a web site and android application?

I’m developing an Android and web application that will function as a service (use the same data). My question is how should the data be stored to allow for both the web and the android application to have access to the same set of data? Should the android application connect to the sites MySQL server to store/access data? If so how do I allow someone to access the data when they are not in ...


Android 1.6 SMS (older application code)

I have HTC Tattoo with Android 1.6. I have maed a Java program for SMS sending. I got the source on the Internet, I think, versions before 1.6. The problem is: SMSs are sent twice. What is a possible cause for this problem? If possible, please simply post sample code what works OK.


How to close Android application?

I want to close my application, so that it no longer runs in the background. How to do that? Is this good practice on Android platform? If I rely on the "back" button, it closes the app, but it stays in background. There is even application called "TaskKiller" just to kill those apps in the background.






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top