Overview button switches activity - java

I have 2 activities Activity A and Activity B. My app starts in Activity A and I have a button on Activity A that leads to Activity B. When I'm in Activity B, I press the Overview Button(The square button) and select my app. When my app loads up, Activity B is destroyed and it loads up into Activity A instead. What is going on and how do I get it to save my settings and load into Activity B?
By extension i'm having an app that loads into firebase authentication and then operates on a firebase database. How do I save the state of it when the overview button is pressed? Do I need to save anything with firebase authetication or do I just save all the data I need to access and manage the firebase database with that?
I watched a couple videos on it where they are talking about screen rotations and saving states, but there's little information about the overview button.
Thanks,
Reinaldo

What about saving the current activity to a value in shared preferences and then checking the value in the onCreate method of activity A. You can have a switch statement that redirects to whichever activity the user was on before.
activity A would look like this:
public class A extends AppCompatActivity {
private String currentActivity;
private final String CURRENT_ACT = "current activity";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
currentActivity = sharedPreferences.getString(CURRENT_ACT, "A");
switch (currentActivity){
case "A":
break;
case "B":
Intent intent = new Intent(this, B.class);
startActivity(intent);
break;
}
}
}
and B would look like this:
public class B extends AppCompatActivity {
private String currentActivity;
private final String CURRENT_ACT = "current activity";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentActivity = "B";
SharedPreferences sharedPreferences = getSharedPreferences("shared preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(CURRENT_ACT,currentActivity);
editor.commit();
}
}

Related

Change background color of another activity with button

I have two activities main and settings. I want to change the background color of the main using a button that's on settings. I tried using RelativeLayout and get the layout of the main using the id and changing the color when touching the button but it doesn't work. I also tried doing with Intent but I don't know how to do it. Got the code for the settings activity below
public class Settings extends AppCompatActivity {
private Button changeColorButton;
private RelativeLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
changeColorButton = findViewById(R.id.changeColorButton);
layout = findViewById(R.id.mainLayout);
/** Called when the user taps the Settings button */
changeColorButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View V){
Intent intent = new Intent(Settings.this, Main.class);
startActivity(intent);
layout.setBackgroundColor(Color.BLUE);
}
});
}
}
Any help please?
You cannot change views of another activity like that.
You can send an 'extra' parameter with your Intent and read it on Settings activity. You can change the color based on the value you receive via the intent.
Or if its a more permanent setting, you can save it on SharedPreferences and read it on Main.

How to save last visited activity and launch it on start

I have the MainActivity which is welcome activity and some other ones.
Lets suppose Act1, Act2 and Act3.
I want the program automatically redirect from MainActivity directly to last visited activity on every app launch.
whether Act1, Act2 or Act3
Well, I tried to do this jump from main activity to last visited activity, but it didn't work.
here's code what I've already tried and didn't work:
MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loadLocale();
setContentView(R.layout.activity_main);
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
int restoredLevel = prefs.getInt("level", 0);
if (restoredLevel > 0) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
}
}
Act1:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Act1);
SharedPreferences.Editor editor = getPreferences(Context.MODE_PRIVATE).edit();
editor.putInt("level", 1);
editor.apply();
}
(I simply want to change launcher activity depending on last visited activity, if you know any better solution for my problem, I will be thankful)

How to access getIntent() from extends AppCompatActivity to extends Application

So, my Activity A is :
public class imei extends AppCompatActivity {
//variables
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imei);
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(Long.parseLong(IMEI) == IMEI2){
IMEI .equals(IMEI2);
textView.setText("IMEI NUMBER : " + IMEI);
Bundle bundle = new Bundle();
bundle.putBoolean("key", true);
Intent intent = new Intent(imei.this, menu_utama.class);
intent.putExtras(bundle);
imei.this.startActivity(intent);
}else{}
now i want to pass value the Boolean value from bundle to Activity B :
public class MyApplication extends Application {
private BeaconManager beaconManager;
//variables
#Override
public void onCreate() {
super.onCreate();
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
final boolean success = bundle.getBoolean("key");
..code
if(success){
do something
}else{do something
but the getIntent() is red, but when i change to extends AppCompatActivity, getIntent() correct. somehow it has to do with onCreate(Bundle savedInstanceState) and extends Application what should i do to fix this
so my problem is how to passing value between activity class to aplication class, first try i'm using Bundle which is using getIntent(), but it did not work because getIntent() is in activity class, because i want to using in application class, so i'm using shared Preference and it works.
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("masuk", true);
// Save the changes in SharedPreferences
editor.apply(); // commit changes
put it in activity class and get the shared preferences in application class:
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("masuk", true);
// Save the changes in SharedPreferences
editor.apply(); // commit changes
and then just use if(masuk){do what you want here}else{do what you want here}
Solved.

how to save EditText's value after performing an other activity

i have my first activity with two EditText fields with hint's as first name and last name, when ever i go to my second activity and return to my first activity by a widget button the EditText fields in my first activity gets reset.
SIMPLY i want my EditText fields to remain same in my first activity as i re-visit my first activity from my second activity.
MY MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButton1Click(View v)
{
Intent intent = new Intent (this,MainActivity2.class);
startActivity(intent);
}
}
My MainActivity2.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
SOLUTION - i changed my second activity to this. just added reference to my Button and applied an onClickListener with onBackPressed(); code.
setContentView(R.layout.activity_main2);
Button button = (Button)findViewById(R.id.button3); //gave reference of button in second activity
button.setOnClickListener(new View.OnClickListener() { //and applied an onClickListener with code onBackpressed();
public void onClick(View v) {
onBackPressed();
// Code here executes on main thread after user presses button
}
});
}
Really Really thnx to #Narendra Sorathiya for his guidance.
Open secondActivity like below,
Intent i = new Intent(A.this,B.class);
startActivity(i);
finish();
to
Intent i = new Intent(A.this,B.class);
startActivityForResult(i,1);
do not open 1st Activity from 2nd Activity, just need to back press.
try this
declare all variavle at top of activity
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String FNAME = "fname";
public static final String LNAME = "lname";
SharedPreferences sharedpreferences;
write this code when you move to your next activity
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
// store login data in sharedpreferences
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(FNAME, edittext.getText());
editor.putString(LNAME, edittext2.getText());
editor.commit(); //save data in sharedpreferences
write this code in your oncreate method after you bind your controls
and get data from sharedprefrence like this
String fname= prefs.getString(FNAME, "");
String fname= prefs.getString(LNAME, "");
than set it to your editext like this
editext.setText(fname);
editext2.setText(lname);
let me know in case of any query

How do I create a custom preference that uses an existing activity that returns a result?

The Preference class allows for an Intent to be set, to have a preference activate another activity when clicked, but I'm unable to find a way to handle the result from an activity using this method. There's also the DialogPreference where I can provide a custom view, but I don't have direct access to the view I want to use, only an activity.
Digging a bit further, it looks like the RingtonePreference uses a few package internal methods on PreferenceManager to receive results from a started sub-activity, but as these are package internal I am unable to do the same.
Is there any other way of handling a custom preference with an activity that returns a result (where the result is to be saved as the value of the preference)?
I have also noticed PreferenceActivity does not return onActivityResult. That being said, is there a reason your SubActivity couldn't save the preference directly? If you need to check the value of it, you could check it at onResume of your PreferenceActivity as a workaround..
//SubActivity onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.Button01);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences prefs = getSharedPreferences(TestPreferenceActivity.PREFS_FILE, MODE_WORLD_READABLE);
prefs.edit().putString("mykey", "someValue").commit();
finish();
}});
}
//PreferenceActivity onResume
#Override
protected void onResume() {
Log.d(TAG, "Preferences Resumed");
//Check for new Preference Values
SharedPreferences prefs = getSharedPreferences(PREFS_FILE, MODE_WORLD_READABLE);
String value = prefs.getString("mykey", "defValue");
Log.d(TAG, "Current value is: " + value);
super.onResume();
}

Categories