Resuming last activity on icon press to set result - android

I am trying to re-open last activity on icon press. But it starts from splash when I remove launchMode SingleTask from main.
Below is the scenario -
A = Splash , B = Activity1 , C = Activity 2.
I want to launch C on icon press.
On Start
A ---->B---->C (Here home icon pressed)
On Click Icon It runs A but I want C . Can any one solve this?
Launching C-
Intent urlIntent = new Intent(this,C.class);
urlIntent.putExtra("stt",Str);
startActivityForResult(urlIntent,REQUEST_CODE);

CASE I. Check if your app is in background after pressing the home button. If it is then it should open activity C.
CASE II. If your app is killed due to low on memory then it will start from launcher activity i.e. activity A.
I also faced this issue. This issue occurred due to changed setting of device.If user enable "Don't keep activities" then all activities will be destroyed if you press home or start new activity.
Note: I used the default launch mode

Your app will always start from the Activity with the filter intent.action.MAIN in the Manifest.xml.
And if your application is already running then the next time time your app starts it will automatically resume from the last opened activity.
Now if your app gets killed or swiped out then you can store the activity in SharedPreferences when activity is resumed or paused and on next start check in your spash which is Activity 'A' in your case, which was the last opened activity as you had stored the Activity name in SharedPreferences.
Example :
If you have three activities A-->B-->C
store the name in onPause() of all activities use SharedPreference to store its name :
#Override
protected void onPause() {
super.onPause();
SharedPreferences prefs = getSharedPreferences("Pref", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("lastOpenedActivity", getClass().getName());
editor.apply();
}
And in your Splash which is here Activity A use this preference to checfk in your onCreate() -
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Class lastActivity;
try {
SharedPreferences prefs = getSharedPreferences("Pref", MODE_PRIVATE);
lastActivity = Class.forName(prefs.getString("lastOpenedActivity", ParentActivity.class.getName()));
} catch (ClassNotFoundException ex) {
lastActivity = ParentActivity.class;
}
startActivity(new Intent(this, lastActivity));
this.finish();
}

Related

Android Studio: Repoen App with Previous Screen

I'm writing an app with 8 different Activities that are all interconnected(accessible from one another). I'm wondering how I can have the app always reopen from a full close (not just home button click but from closing the background activity) with the same screen it was exited from.
For example, from my Splash Screen I navigate to a home screen. From that home screen I navigate to the Settings. Now, I click the multi-tab viewer and close all apps. How can I have Settings(or wherever I had left off) become the launching activity when the app is reopened?
Thanks in advance!
You can use SharedPreferences to store the class name of the latest Activity. Later, when relaunch, you can redirect to it from the Splash Screen.
You can do that simply by using SharedPreferences. When you launch an activity, it changes it's last activity value in SharedPreferences to point at that activity.
When you close the app and then reopen it, your Main/Launcher activity (Which may be your Splash Screen) then checks for this value and launches the activity according to this value which represents the last activity the app was on.
Here is the code that would fit the Main/Launcher activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String lastActivity = getDefaults("LAST_ACTIVITY", MainActivity.this);
Intent intentLastActivity;
// use an if statement to find out which activity it is
if(!lastActivity.equals("") || !lastActivity.equals(null)){
if(lastActivity.equals("home")){
intentLastActivity = new Intent(MainActivity.this, HomeActivity.class);
}else if(lastActivity.equals("settings")){
intentLastActivity = new Intent(MainActivity.this, SettingsActivity.class);
// add more else-if statements for the other activities.
}
startActivity(intentLastActivity); // launch the last activity
finish(); // finish/close the current activity.
}
}
public static void setDefaults(String key, String value, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply();
}
public static String getDefaults(String key, Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(key, null);
}
Once that code is placed in your Main/Launcher activity. You can then set this last activity value in SharedPreferences in other activities when they are launched.
Code placed in the other activities (in the onCreate method):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
// we are at the settings activity. Change last activity value to settings
MainActivity.setDefaults("LAST_ACTIVITY","settings",this);
}
Luckily the methods to change the last activity are accessible in other activities.
If you're able to implement this well in your app. It should work as you want it to.
HOPE THIS HELPS!

Open Splash activity once

I am opening splash activity once, this is what i have done.
public class StartupActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(StartupActivity.this);
String lang = settings.getString("opened", "");
if(opened.equals("1")
{
Intent i = new Intent(getApplicationContext(), SecondActivity.class);
startActivity(i);
finish();
}else { //I am setting here opened to 1
setContentView(R.layout.activity_main);}
}
Where does the problem lie? When I open the app first time, this activity shows setcontentview activity main, but second time user opens app it goes to secondactivity .But the problem is , for couple of milliseconds the second time i open the app it is making it obvious that splash activity is opening then closing for couple of milliseconds.
I believe your splash is the launcher activity of your application and what you are trying to do is , once you have launched the splash and passed the value to second activity then splash shouldn't show .
I saw a similar question long ago , check if it answers it
App restarts rather than resumes

how to start from different Activity in android studio

i make app with two activities.
firs has:
2 ExitText(login and password);
button(confirm, save data with SharedPreferences, intent to second activity).
second one:
2 TextView(get login and password with SharedPreferences);
button(clear data on SharedPreferences, intent to firsActivity).
how to make next: while there are some data on SharedPreferences - app will be started from the 2nd screen.
for example, i made:
if (user!=null && pass!=null){ Intent enterIntent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(enterIntent);
}
but, technically it first run the firstActivity and than go to the secondOne. if there are some method to start app with the another activity (not mainOne)?
You won't be able to check to see if there are values in SharedPreferences before entering one Activity.
What you can do is check the value before displaying the UI (before calling setContentView(R.layout.my_layout)), and either continue along, or start the next Activity.
public class MyStartActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
if (preferences.contains("my_key")) {
// start next Activity
}
setContentView(R.layout.my_layout);
}
}
If you don't want the first one on the back stack, you can call finish() after starting the second one (or use appropriate flag(s) on intent).
Another approach would be to have only one activity with to fragments and decide dynamically which one set on start. With fragments you can also easily change layouts on button click or on back pressed.
I'm not sure if it will work, because app has to start at the first activity. First activity checks login and pass at share preferences and then you can go to the second activity
try this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preference = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
if (preference.getSting("key",null)!= null) {
// start new Activity
//finish this activity so it not in back stack
} else {
setContentView(R.layout.my_layout);
}
}

How to display different content depending on whether the app is opened first time?

How to display different content depending on whether the app is opened first time or the user setup something?
Imagine a activity A, B, If the user open the app first time or haven't setup something in activity B, Then display activity B, otherwise display activity A.
How to overcome this? Thank you.
Use PreferenceManager. In activity A add check for the preference firstLaunch. If it's not set, launch activity B. Then, in activity B, set it.
It could look like:
Activity A
#Override
protected void onResume() {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
// By default assume "true", which means this is the first launch.
if (prefs.getBoolean("firstLaunch", true)) {
// Start Activity B and finish myself.
Intent i = new Intent(getApplicationContext(), ActivityB.class);
startActivity(i);
finish();
}
}
Activity B
// Do it after you've initialized everything, so Activity A can be launched:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this)
SharedPreferences.Editor editor = prefs.edit();
// Set "fistLaunch" to "false", so Activity A will start next time.
editor.putBoolean("firstLaunch", false);
editor.commit();
If you want to show different activity if user opened the app first time and different activity second or third time and so on then you can use below approach.
You can use the SharedPreference to achieve this.
Check the condition like this in the Splash Activity :
SharedPreferences preferences = getSharedPreferences("prefName", MODE_PRIVATE);
if(!preferences.getBoolean("isFirstRun", false)) {
// This mean App Launch First Time
SharedPreferences preferences = getSharedPreferences("prefName", MODE_PRIVATE);
SharedPreferences.Editor edit= preferences.edit();
edit.putBoolean("isFirstRun", true);
edit.commit();
// Open the B Activity
} else {
// This mean App Launch Second or third ... time and start the A Activity
}

Perform a task only at start of an Android application

In my app I want a string to be passed to the server only at the startup of application.
I have used SharedPreferences to check if the string is sent or not.
this code runs at star of MainActivity
pref=getPreferences(MODE_PRIVATE);
Sent=pref.getString("ID_SENT", "");
if(Sent.equals(""))
{
SharedPreferences.Editor editor=pref.edit();
editor.putString("ID_SENT","NO");
editor.commit();
Sent=pref.getString("ID_SENT","");
}
Now after the string is sent to the server in onPostExecute of AsyncTask this code is used to set the variable ID_SENT to YES:
Sent=pref.getString("ID_SENT", "");
if(Sent.equals("NO"))
{
SharedPreferences.Editor editor=pref.edit();
editor.putString("ID_SENT","YES");
editor.commit();
}
Now the problem is when I close the app and start the app again it doesn't send the string to server because it finds ID_SENT set to YES.
So is there any way so that I can set ID_SENT to no as the back button is pressed while on MainActivity?
EDIT:
I set launch mode of MainActivity to singleTop. So now activity resumes whenever I press home button from another activities. And the string is not sent everytime because MainActivity is only resumed.
So is there any way so that I can set ID_SENT to no as the back button is pressed while on MainActivity?
Yes,
#Override
public void onBackPressed()
{
// do your save logic here
}
Or you could override onDestroy(), this may be safer in case Android destroys the Activity to reclaim memory

Categories

Resources