jump from main activity to last visited activity - android

Lets suppose I am at 4th activity of my app and I have ten activities. I close the activity from 4th level now when i reopen my activity again
i have a button on main activity when i click on it I must have to go on 4th activity where i left last time. Now what I do is that I saved every activity number or ID in Shared preference like that
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level1);
SharedPreferences.Editor editor = getPreferences(Context.MODE_PRIVATE).edit();
editor.putInt("level", 4);
editor.apply();
Then i retrieve it in main activity like this:
btnconti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
int restoredLevel = prefs.getInt("level", 0);
if (restoredLevel >0) {
}
}
});
Now can any body tell me how i can jump to my last visited activity?

If you have seperate Activities for all 10 activities u can call an intent as follows inside multiple if conditions of restoredlevel
Intent i = new Intent(getApplicationContext(),ActivtyOfyourlevel.class)
startActivity(i);

Related

can I change launcher activity in MainAvtivity

How can I change Launcher activity in android studio using Button click in Java file??
for example, if user clicks Button1 then a activity will be launcher activity and if user clicks Button2 then another activity will be a launcher activity???
Which activity is launched at startup is determined by the AndroidManifest.xml file, you cannot modify this at runtime.
However, you can make an activity whose sole purpose is to launch the app and then choose which other activity to launch:
in button 1 click, save activity 1's name into a sharedpreferences entry
in button 2 click, save the other's name
in the launch activity, check the value of the sharedpreferences entry and launch an intent with the correct activity based on that
Edit: example for what you are trying to do
In your Button1 click:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// get the sharedpreferences with the name "myPreferencesName"
// and the mode "MODE_PRIVATE" (i.e. only you can read the settings stored in it)
// and then get its editor
SharedPreferences.Editor editor = getApplicationContext().
getSharedPreferences("myPreferencesName", Context.MODE_PRIVATE).
edit();
editor.putString("activityToLaunch", "activity1"); // activity1 can be whatever string you choose, not neccessarily the actual name of the activity
editor.apply();
}
});
In your button2 click, you do the same but put "activity2" as the preference value
In your MainActivity onCreate:
SharedPreferences prefs = getApplicationContext().
getSharedPreferences("myPreferencesName", Context.MODE_PRIVATE);
if(!prefs.contains("activityToLaunch")) {
// neither button has been pressed since you installed the app
// this always happens on the first launch after installation
// do the editor thing here as you would in the button click
// depending on which activity you want to be default, either set the preference value to "activity1", or "activity2", then editor.apply() it
}
if(prefs.getString("activityToLaunch").equals("activity1")) {
Intent intent = new Intent(getApplicationContext(), Activity1.class);
startActivity(intent);
}
else {
Intent intent = new Intent(getApplicationContext(), Activity2.class);
startActivity(intent);
}

How can I get the name of the previous activity and store in a variable

I'm developing an app where I have many activites. And when the user ends interacting with the content of each activity it launchs an Activity called WellDone! where the user can see the average of the lessons (it's a course). 100% completed and it has a button for the next lesson. I want to get the name of the previous activities for example activity_lesson1, activity_lesson2 to show in a TextView to have a Title.
When ends the lesson1 it launchs the WellDone! Activity and I want to set in a TextView (title) the name of the lessons that have just learned.
package com.flixarts.ar.**;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class welldone extends AppCompatActivity {
private TextView title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pantallanextsesion);
title = (TextView)findViewById(R.id.tituloActivity);
title.setText() //Here the title of the previous activity that have come from intent
}
}
My impression is that the answer to the literal question is "no" - you cannot get the name of previous activity. But there's probably another way to accomplish what you need.
If these are all your activities, why not put an extra in the Intent (when going in the forward direction) which says which activity it is coming from.
You can then decide what name of the activity to display based on checking that extra, which should tell you what the previous activity was.
Maybe you could use SharedPreferences, which allow you to easily save some String value in one activity and read it in another. In previous activity e.g activity_lesson1 you could override method onPause (or onDestroy if you finish your activity while proceeding to next one by calling finish() method) and save the name of activity as String like this:
#Override
public void onPause() {
super.onPause();
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("LAST_ACTIVITY_NAME", this.getClass().getSimpleName());
editor.apply();
}
Then you can read saved value inside WellDone activity like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
String lastActivityName = sharedPref.getString("LAST_ACTIVITY_NAME", "default_value");
title.setText(lastActivityName);
}
Two ways to pass previous activity name
Use Intent
FirstActivity
String TAG=FirstActivity.class.getSimpleName();
submitBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name=nameEt.getText().toString();
mobile_number=mobileEt.getText().toString();
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("Last_Activity_Name",TAG);
startActivity(intent);
}
});
SecondActivity
String lastActivityName;
lastActivityName=getIntent().getStringExtra("Last_Activity_Name");
**use SharedPreferences **
FirstActivity
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("Last_Activity_Name", TAG);
editor.apply();
SecondActivity
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
String lastActivityName = sharedPref.getString("Last_Activity_Name",
"default_value");
title.setText(lastActivityName);

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!

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
}

Categories

Resources