Save settings button and back to previous activity - java

I have an activity that has edit text and you can type your name in it, and when you click Save button you will be redirected to MainActivity, but I don't want to open MainActivity by Intent, I make that save button save your name with shared prefs and everything works fine but I don't want to open my Main in Intent I want that when I clicked on save button the current activity that saves data close and previous activity open.
sorry for my bad English.
this is my code for save button
submitButt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == submitButt.getId()) {
String name = inputName.getText().toString().trim();
if (isValidInput(name)) {
Intent setint = new Intent(SettingsActivity.this, MainActivity.class);
setint.putExtra("name", name);
saveData();
startActivity(setint);
Toast.makeText(SettingsActivity.this, "Settings Saved", Toast.LENGTH_SHORT).show();
}
}
}

Then you just open the PreviousActivity instead of the MainActivity. Add a checker inside the PreviousActivity if "name" has data or not. You can also use finish(); this will kill the activity.
** Update **
In your MainActivity, let's say you have TextView name. Add:
#Override
public void onResume(){
super.onResume();
// Check your variable if it has value or none
textViewName.setText(variableForSharedPref)
}
Then you can proceed with usual process to go in Settings and Go Back to MainActivity.

Related

TextView from SecondActivity become visible after a button from MainActivity is pressed?

I'm very new to Android Studio Development and I was wondering how to do this, when I click a button on MainActivity, it will direct me to secondActivity where the text become visible (Originally TextView will not be visible until the button from MainActivity is pressed)
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
String status = "Success!";
intent2.putExtra("Status",status);
startActivity(intent);
}
});
I want to make an if-else statement for this (on SecondActivity page) where if user straight away go to SecondActivity, it will not display any text there. But if pressed the button on MainAcitivty page, the system will go to SecondActivity with the TextView displayed.
Thanks!
Basically, there are several approaches you can do that.
Use intents pass data
Sure you pass a boolean type or whatever you want into this intent, I think this is the approach you are trying to make here. So I can give you an example:
In your first activity you can do something like this,
button.setOnClickListener {
val intent = Intent(this#MainActivity, SecondActivity::class.java).apply {
val status = true
putExtra("Status", status)
}
startActivity(intent)
}
And in your second activity, in your need to override onCreate to parse your intents to decide your text want to display or not.
val status = intent.extras?.getBoolean("Status")
if(status) {
hideText()
} else {
showText()
}
the other approach you can deal with it is try to create singleton class to keep the status in this class, and based this singleton class status, you may choose to hide/show your text. However this solution isn't the recommended way to do it. Because global state is bad for testing and just pollute the code.

Call a Method from another Method Android

I am not sure this workaround is the correct way to achieve my goal of having a prompt text in a spinner. What happens with this application is the spinner navigates to another Activity via an Intent and when the user navigates back to the Main Activity with the spinner they have two ways back. One with a Button and a click event the other by clicking the device BACK button. I am trying to call the code in the click event from the method that manages the device BACK button
I do not know how to call the click event from the device BACK button Method
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Use BACK BUTTON\n\n"+"On the Screen",Toast.LENGTH_LONG).show();
// I want to call goBack(View view) from here
// +++++++++++++++++++++++++++++++++++++++++++
}
public void goBack(View view){
Intent i = new Intent( PageTwo.this, MainActivity.class );
startActivity( i );
}
The reason I use this Intent to navigate BACK to the Main Activity is it reloads the variables in the Spinner
It looks like goBack(View) is most likely from an onClick setup in your layout XML. Since you aren't using the view, just pass null:
#Override public void onBackPressed() {
goBack(null);
}
I don't know if I get you right, if you just want to go back to the activity which started another activity, you can just call finish() method of Activity class:
#Override
public void onBackPressed() {
finish();
}
finish() reference

Add to Favourites Button SQLite in Android

I am setting up an application and need to have a favourites button in an activity to add the activity to a favourites table. I have a Facebook Login set up with SQLite database storing the email. I was wondering how to save an activity to my favourites and have it stored in the SQLite Database?
I have seen code such as:
btnAddFavourite = (ImageButton) findViewById(R.id.btnAddFavourite);
btnAddFavourite.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Add code here to save the favourite, e.g. in the db.
}
});
btnAddFavourite.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// Open the favourite Activity, which in turn will fetch the saved favourites, to show them.
Intent intent = new Intent(getApplicationContext(), FavViewActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(intent);
return false;
}
});
But I am not sure what code to add in to be able to save the activity to my favourites in the SQLite Database.
Possible Solution - I believe SharedPreferences could be used but how do you save the whole activity?
Therefore when the user goes to their favourites page, they have a button to go to that activity they saved
I would really appreciate your help.
Thank you so much in advance!
your solution as i understand will be that you can get your activity name and save it in db. after that when you want to return to that activity get the name of activity and after that start activity with its name.
to get name of activity use the this.getClass().getSimpleName();
and to start activity from its name as string use : startActivity(this, Class.forName(yourStringClass));
for more information about getting name of activity and start activity from string name refer to links bellow:
Get Activity name dynamically - android
Intent and start activity from string

How to go another Activity using function instead of Button widget

I hope if someone help me in this ,
in my projet I am trying to open another avtivity used voice command Ex, say "one " it should compare if the string is equal to what I? have it should switch to another activity or external activity such as Phone call, Camera. What I did here, I store the recognized ward in Edittext and store it in string and compare it . my 2 projects counted on how to do this
Inside onCreate:
final Button btntx = (Button)findViewById(R.id.btn1);
final EditText edittxt1 = (EditText)findViewById(R.id.edttxt);
btntx.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent (Intent.ACTION_DIAL); // sow the Dial window
intent.setData(Uri.parse("tel:"));
startActivity(intent);
);
}
////////////////////////// outside the onCreate //////// Function after I get the result from the Speech recognizer and call this function to see if the what it said is equal to my keyword . SO i need to call the button to open that activity
String gwdata= edtxt.getText().toString(); // data from EditText
if (gwdata.equals("One"))
{
Toast.makeText(getBaseContext(),"They are equal", Toast.LENGTH_SHORT).show();
// Here Do someting to go to another activity
}
Thnks guys in advance .

stopping toast android in other actvity

my problem is this:
I make a toast "welcome" in the Activity Main for the first time that you open the home, and it's ok,but when another page to return to the home via the back button, how can I make the toast "welcome" does not appear anymore?
the code of main activity is:
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
MyActivity actvi1;
int cont=0;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnabout=(Button)findViewById(R.id.about);
//click
btnabout.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// definisco l'intenzione di aprire l'Activity "aboutme.java"
Intent aboutmejava= new Intent(MyActivity.this,aboutme.class);
startActivity(aboutmejava);
}
}
);
//toast
Toast toast = Toast.makeText(getApplicationContext(),
"Welcome", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 20, 0);
toast.show();
code of aboutme.java
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.aboutme);
Button btnback=(Button)findViewById(R.id.scritta);
btnback.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent main = new Intent(aboutme.this,MyActivity.class);
startActivity(main);
If you want it to only show the very first time the application runs, put a boolean flag in SharedPreferences and check here. There are tons of examples but here is one
If you want it to show *every time the Activity is first run when the app starts, simply replace your onClick() code with onBackPressed(). This way it won't start a new instance of your MyActivity and since the Toast code is in onCreate() and not onResume(), it won't run when you go back by clicking the back button.
SharedPreferences
This works.
SharedPreferences sp = getPreferences(Context.MODE_PRIVATE);
int show = sp.getInt("firstlaunch", 0);
if(show == 0) {
Toast.makeText(getApplicationContext(), "WELCOME", Toast.LENGTH_LONG).show();
sp.edit().putInt("firstlaunch", 1).apply();
}
Place it in your home activities onCreate method.'
A shared preference is a "setting" of sort. It's a xml file that is loaded which contains all of your settings. When we first run "sp.getInt" you can see that i have a 0 after the "key - fistlaunch". The 0 is specifying what to give our Int SHOW if it can't find any shared preference with that key. Next if the int show is equal to 0 we run our Toast and then change the shared preference value to 1 so next time you run it doesn't show...
boolean b = false;
if(!b) {
Toast toast = Toast.makeText(getApplicationContext(),
"Welcome", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER_HORIZONTAL, 20, 0);
toast.show();
b = true
}
kind of like that?
Are you another instance of MainActivity?
In order to return to your Main activity you should send an Intent with the flag FLAG_ACTIVITY_CLEAR_TOP (or set android:launchMode="singleTop" in launchMode in AndroidManifest.xml)
Intent main = new Intent(aboutme.this,MyActivity.class);
main.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(main);
By doing so, your main activity will be restored, instead of creating a new instance of MainActivity

Categories

Resources