How to go back to the previous activity? - java

I am developing an application with 5 interfaces
I have an activity where the user has to enter a password, after entering it the user moves to the next activity, but when the user clicks on back button and he's returned to the password activity, the entered password is missing... how can I maintain the entered password?
Code:
back = (Button) findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View v1) {
Intent backIntent = new Intent();
backIntent.setClass(getApplicationContext(), LockAppActivity.class);
backIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(backIntent);
finish();
}
});
Suppose the app starts with activities A --> B --> C --> D --> E
When user moves to B from C, and at activity B there is a "deactivate" button and when the user presses it all the user settings are cleared and the application will exit, but when I press the button, it brings me to the layout C... how can I change this?
deactivate = (Button) findViewById(R.id.deactivate);
deactivate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
final String STORAGE = "UniqueID_IMSI";
SharedPreferences unique = getSharedPreferences(STORAGE, 0);
SharedPreferences.Editor editor1 = unique.edit();
editor1.putString("identifier", "");
editor1.putString("simIMSI", "");
editor1.commit();
final String LOGIN_PASSWORD = "Login_Password";
SharedPreferences loginPassword = getSharedPreferences(LOGIN_PASSWORD, 0);
SharedPreferences.Editor editor2 = loginPassword.edit();
editor2.putString("loginPassword", "");
editor2.commit();
final String LOCK_APP_PASSWORD = "LockAppPassword";
SharedPreferences userPassword = getSharedPreferences(LOCK_APP_PASSWORD, 0);
SharedPreferences.Editor editor3 = userPassword.edit();
editor3.putString("password", "");
editor3.commit();
final String PHONE_NUMBER = "Phone_Number";
SharedPreferences phone = getSharedPreferences(PHONE_NUMBER, 0);
SharedPreferences.Editor editor4 = phone.edit();
editor4.putString("phoneNumber", "");
editor4.commit();
finish();
}
});
}

how can i maintain the entered password
Save the password entered by the user in a SharedPreference and in onResume() function of the Activity recalled the saved password from the SharedPrefrence and show it to user.
You may like to spend sometime trying to understand the Activity lifecycle.
application will exit
Have a look at this link. #Romain Guy, is a Google guy. You may wish to rethink about your app design.

my favorite solution to the closing-app problem are:
consider using "startActivityForResult" for activities that you wish to close when a specific event has occurred .
you can also use some intents flags , but i consider them quite messy and try to use as little of them as possible
use fragments instead of activities . you can choose exactly which fragment to show and when.

Related

Check activation code on first app run

I created an android app that when it runs for the first time an activation page loads with these codes .
Button btnInput = (Button) findViewById(R.id.send);
btnInput.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
EditText m = (EditText) findViewById(R.id.editText);
String strInput = m.getText().toString();
if (strInput.equals("123") || strInput.equals("456"))
startActivity(new Intent(ActiveCode.this, MainActivity.class));
else
startActivity(new Intent(ActiveCode.this,ActiveCode.class));
finish();
}
});
}
And Main activity code like this :
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// second argument is the default to use if the preference can't be found
Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);
if (!welcomeScreenShown) {
startActivity(new Intent(MainActivity.this,ActiveCode.class));
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(welcomeScreenShownPref, true);
editor.commit();// Very important to save the preference
}
}
the problem is that when on first run user closes the app completely without entering code and open it again they don't need to enter code and it goes directly to main activity , so basically it's a bug of my app .
I want help to change codes in a way that activation page disappear only when user enters code.
Try to initialize preference like this,
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
instead of,
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
Because your preference is not binding with your activity.

Call View from separate Activity

I have 2 Activitys "MainActivity" and "MainActivity2". One Admob-Banner is shown in both Activitys at the bottom.
My Problem: I want to disable both Banners by pressing a button in my MainActivity. But as I am new to android and development in general I lack in experience. I search the internet but could not find a valid answer for my problem.
My Question: Is there a way to link both Ad-ids from separate Activitys in my method and what would be the best approach?
This is the method I call from MainActivity so far:
private void hide() {
//This is the Ad from MainActivity
final AdView adLayout = (AdView) findViewById(R.id.adView);
final Button disableAds = (Button) findViewById(R.id.disableAds);
runOnUiThread(new Runnable() {
#Override
public void run() {
adLayout.setEnabled(false);
adLayout.setVisibility(View.GONE);
disableAds.setEnabled(false);
disableAds.setVisibility(View.GONE);
}
});
}
Do one thing when every you press the disable button save the state in Shared preferences. In Each every activity onStart() method check the state of the value. based on that value show/hide the ad-banner in your activity.
String MyPREFERENCES = "myPrefs" ;
SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
Set the Value when you click the button
Editor editor = sharedpreferences.edit();
editor.putString("show_ads", "no");
editor.commit();
Then onStart() method get the value of "show_ads", based on value show/hide the ADs.
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
String name=sharedpreferences.getString("show_ads", "yes");
if(name.equals("yes")){
adLayout.setEnabled(true);
adLayout.setVisibility(View.VISIBLE);
disableAds.setEnabled(true);
disableAds.setVisibility(View.VISIBLE);
}else{
adLayout.setEnabled(false);
adLayout.setVisibility(View.GONE);
disableAds.setEnabled(false);
disableAds.setVisibility(View.GONE);
}
If you are calling "MainActivity2" from "MainActivity":
//Code in "MainActivity" while starting "MainActivity2":
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
intent.putExtra("isAdDisabled", true);// pass true if ad is disabled otherwise false
startActivity(intent);
//Code in onCreate of "MainActivity2":
Intent intent = getIntent();
boolean isAdDisabled = intent.getBooleanExtra("isAdDisabled", false);
if(isAdDisabled){
// code to hide adview
}
Use a shared preference to store a boolean (isAdsDisabled) in your main activity when you click a button.
Editor editor = context.getSharedPreferences("ADS_PREF", Context.MODE_PRIVATE).edit();
editor.putBoolean(ADS_DIABLED, isAdsDisabled);
you can query this again when you want to show the ad in the second activity to decide whether it should be displayed or not.
you can do this by using
SharedPreferences preferences = context.getSharedPreferences("ADS_PREF", Context.MODE_PRIVATE);
boolean isAdsDisabled = preferences.getBoolean(ADS_DIABLED, false);
you can check the value of isAdsDisabled to decide it.

How to save user input from EditText to a variable to be used on a different Activity

I have an edit text in Android Studio which I want a user to enter a name which can then be stored as a variable. I then want to add this variable to a textview in my main activity.
Right now all a have is a blank activity with an edit text and button to save the user input as a variable.
Edit
Ok, I am going to change my approach. In my main activity, I have two text views. If I changed them to edit texts, then how would I save them from the main activity without a save button so that what the user typed would still be there?
Save it in Shared Preferences and then retrieve it from the other activity.
To save a String in shared preferences, you can create a method like the following:
public static void setUsername(Context context, String username) {
SharedPreferences prefs = context.getSharedPreferences("myAppPackage", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("username", username);
editor.commit();
}
To retreive it :
public static String getUsername(Context context) {
SharedPreferences prefs = context.getSharedPreferences("myAppPackage", 0);
return prefs.getString("username", "");
}
Edited:
In the activity which contains the EditText, you can call the method as follows:
setUsername(this,myEditText.getText().toString());
And in the Activitiy that contains the TextView:
myTextView.setText( getUsername(this) );
you ça use edit.getText().toString() tout get the user input.
To launch new activity and pass the string to it use
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("key", theString);
startActivity(intent);
EDIT
To be more easy you should add a button to your layout so when the user press that button you can launch the next activity with the string.
This should go into your activity
public class BlankActivity implements OnClickListener {
private EditText mEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
//find editText
mEditText = (EditText) findViewById(R.id.your_edit_text_id);
//listen button
findViewById(R.id.your_button_id).setOnClickListener(this);
}
#Override
public void onClick(View v) {
String theString = mEditText.getText().toString();
Intent intent = new Intent(this, OtherActivity.class);
intent.putExtra("key", theString);
startActivity(intent);
}
}

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 .

Login Directly second time

I want to create Login page that user login only first time. When user want to use app second time they dont have to encounter login page. my code is here
loginPreferences = getSharedPreferences("loginPrefs", MODE_PRIVATE);
loginPrefsEditor = loginPreferences.edit();
saveLogin = loginPreferences.getBoolean("saveLogin", false);
if (saveLogin == true) {
name.setText(loginPreferences.getString("username", ""));
pass.setText(loginPreferences.getString("password", ""));
}
login.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
name1 = name.getText().toString();
pass1 = pass.getText().toString();
//new Thread (new Task()).start();
loginPrefsEditor.putBoolean("saveLogin", true);
loginPrefsEditor.putString("username", name1);
loginPrefsEditor.putString("password", pass1);
loginPrefsEditor.commit();
new myAsyncTask().execute();
}
});
This code provides remember user's username and password but still when user want to use app second time they have to encounter login page.
How can i solve this problem.
Thanks for help.
One way to go about this is you could design your app's architecture to set the main portion of your app as the the first activity to launch. Then the first thing that activity could do is check your shared preferences to see if the user has already authenticated themselves. If they haven't, launch the login activity and finish the main activity.
You should do this :
if (saveLogin == true) {
name1 = loginPreferences.getString("username", "");
pass1 = loginPreferences.getString("password", "");
new myAsyncTask().execute();
}
So when you have data in shared preferences you should login user by executing your AsyncTask

Categories

Resources