can I change launcher activity in MainAvtivity - android

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);
}

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!

Android: Using the app home page as launcher activity

I am building a simple facebook login application in android. I am using my login activity as the launcher activity. However, I would like to use my home page as launcher activity if the user is already logged in, otherwise login activity act as the launcher activity. Can anyone tell me the right way to do that?
If the user login, save that data, could be a string value, int, boolean, etc... check the data and if matches, make a staractivity intent.
Use my library to achieve that: KeySaver
In your Login Button (onClickListener) make this:
mLoginButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!KeySaver.isExist(this, "haslogin")){
KeySaver.saveShare(this, "haslogin", true);
}
});
And at the very beginning of your Login Activity make this:
if(KeySaver.isExist(this, "haslogin")){
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(myIntent);
}else{
// do your code for login, etc
}
When logging in first time do this:
SharedPreferences shared;
shared=getSharedPreferences("com.example", Context.MODE_PRIVATE);
shared.edit().putBoolean("loggedin",true).apply();
After you are already logged in
in Your onCreate() of loginActivity do this:
SharedPreferences shared;
shared=getSharedPreferences("com.example", Context.MODE_PRIVATE);
if(shared.getBoolean("loggedin",false)){
startActivity(new Intent(LoginActivity.this,Homepage.class));
finish();
}

jump from main activity to last visited activity

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);

Save Setting onDestroy

How to save setting after we exit the app using onDestroy?
Example:
When the app start, it will start Main_Activity.class
Button button1;
public class Main_Activity extends Activity {
super.onCreate(savedInstanceState);
................
}
Added a button named "button1" and give an Action to open new activity when clicked
public void button1_newactivity (View v){
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener (new View.OnClickListener() {
public void onClick(View arg0) {
Intent secondactivity=new Intent (getApplicationContext(), Second_Activity.class);
startActivity(secondactivity);
}
});
}
Added 2 Checkbox on Second_Activity.class, as a default when the app start checkbox1 is selected and checkbox2 is not selected. But, when checkbox2 selected and checbox1 automatically not selected, after pressed another button it will start Third_Activity.class.
My Question is how can we save this setting, so when we exit the app, then start the app again, It will automatically start Third_Activity.class not Main_Activity.class like the first one?
What should we write in this part
protected void onDestroy(){
....................
}
use SharedPreferences to store which will be your first activity. launch your launcher activity like before. But in there check the value you saved in sharedpreference. So if you find that you have to start 3rd activity from the oncreate of launcher start the third and finish the first one. For example
public class Main_Activity extends Activity {
super.onCreate(savedInstanceState);
SharedPreferences pref = getSharedPreferences(name);
boolean b = pref.getBoolean("should_start_third", false);
if(b){
finish();
start third activity
}
................
}
Here in SharedPreferences i used a should_start_third boolean value to check if the third activity will start directly. this is by default false.
you have to save the value of shared preferences after the third checkbox is selected. to save use like following.
getSharedPreferences(name).edit().putBoolean("should_start_third", true).commit();

Android MainActivity calls another Activities, after pressing back key, blank screen shows

my main.xml file is just dummy. I want to start different activites based on the condition. If the password is found in shared pref file, the login activity should be launched, and if password is not found, the configuration activity should be launched. it is working fine but when I press the back key from keypad, the main activity is shown (I mean the blank screen because there is nothing) How can I avoid this?
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = getSharedPreferences(preffilename, MODE_PRIVATE);
final String password = prefs.getString("password",null);
if(password == null)
{
Intent i = new Intent(getApplicationContext(), Configuration.class);
startActivity(i);
}
else
{
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
}
}
Call finish() from your main activity after calling startActivity(), this will remove main activity from stack.
What is your expectation when you pressed the back button? You might want to put those code in onResume() so it always get called when the main activity is brought back from the stack.

Categories

Resources