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

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.

Related

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)

Overview button switches activity

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

Android: clear activities stack [duplicate]

This question already has answers here:
How to finish Activity when starting other activity in Android?
(7 answers)
Closed 3 years ago.
I have an activities:
Activity1, that should be opened once (first launch)
Activity2, the main screen.
After doing some things in Activity1 on first launch, every next launch Activity2 should be opened.
I kinda figured out how to do this, and it works perfectly, but when I press "back" button on my phone, Activity1 suddenly appears.
So how can I fix it? Should I clear stack or what?
Here's my code:
Activity1:
public class MainActivity extends AppCompatActivity {
Button but1;
EditText input1;
TextView error1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_page);
if(getName("username")!=null){
Intent intent = new Intent(MainActivity.this, WelcomePageLast.class);
startActivity(intent);
}
ListenerOnButton();
}
public void ListenerOnButton(){
but1 = (Button)findViewById(R.id.welcome_page_button);
input1 = (EditText)findViewById(R.id.username_input);
error1 = (TextView)findViewById(R.id.name_error);
but1.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view){
if (input1.getText().toString().length() < 2){
error1.setText("Слишком короткое имя!");
}else {
error1.setText("");
Intent intent = new Intent(MainActivity.this, WelcomePageLast.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);
setName("username", input1.getText().toString());
startActivity(intent);
}
}
}
);
}
public void setName(String key, String value){
SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(key, value);
editor.apply();
}
public String getName(String key){
SharedPreferences preferences = getSharedPreferences("preferences", MODE_PRIVATE);
return preferences.getString(key, null);
}
}
Activity2:
public class WelcomePageLast extends AppCompatActivity {
TextView greetings;
#Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences prefs = getSharedPreferences("preferences", MODE_PRIVATE);
String greeting_message = getString(R.string.greetings_message) + prefs.getString("username", "") + "!";
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_page_last);
greetings =(TextView)findViewById(R.id.greetings);
greetings.setText(greeting_message);
}
}
It won't send you back to last acitivity
Intent intent = new Intent(MainActivity.this, WelcomePageLast.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);
setName("username", input1.getText().toString());
startActivity(intent);
finish();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - Clears the activity stack. If you don't want to clear the activity stack. PLease don't use that flag then.

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

Reload SharedPreferences

I'm quite new to Android Prorgramming and have a problem understandig SharedPreferences.
I have a Main Activity and a Settings Activity.
I want to be able to change a SharedPreference (here called firstWaitTime) in the Settings Activity and work with it in the Main Activity.
I can Set the new value in the settings, but in the Main Activity i only see the new value when i close and reopen the app.
I Think i would need a OnPrefrenceChanged Listender but i have no idea how to implement it...
The Main one looks like this:
public class MainActivity extends ActionBarActivity {
//Radio Button
private int stufe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences settings = this.getSharedPreferences("settings", Context.MODE_PRIVATE);
int firstWaitTime = settings.getInt("firstWaitTime", 12);
int secondWaitTime = settings.getInt("secondWaitTime", 8);
int thirdWaitTime = settings.getInt("thirdWaitTime", 6);
//TEST
final TextView test = (TextView) findViewById(R.id.test);
test.setText("" + firstWaitTime);
}
The Settings Activity looks like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
final SharedPreferences settings = this.getSharedPreferences("settings", MODE_PRIVATE);
//BUTTON
final EditText edit1 = (EditText) findViewById(R.id.edit1);
Button save=(Button) findViewById(R.id.savebtn);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//prefs = getSharedPreferences(prefName, MODE_PRIVATE);
//SharedPreferences.Editor editor = prefs.edit();
SharedPreferences.Editor editor = settings.edit();
//---save the values in the EditText view to preferences---
editor.putInt("firstWaitTime", Integer.parseInt(edit1.getText().toString()));
//---saves the values---
editor.commit();
Toast.makeText(getBaseContext(), "Saved", Toast.LENGTH_SHORT).show();
}
});
}
What is the best way to achieve this?
onCreate() will not be called as Android hasn't killed the Activity. Try putting your code in onResume() instead. For more info read up on the Android Activity lifecycle.

Categories