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.
Related
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.
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);
}
}
I'm working on a new application in which i want create a intro guideline or wizard or something like tha with a viewpager style. I need display this "Intro" part only the first time i open the application and then never more. In the last tab i want a button and when tapped it brings me to the real MainActivity in which execute the operations. I'm able to create the ViewPager following this example ViewPager tutorial but i can't understand how pass from those fragments to my MainActivity and never more display those tabs. I don't know if my question is clear. I can create a button in the last fragment and onClick open the New activity.. But what about never more display the "tutorial" intro? Thanks
EDIT with button:
startAppBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent startApp = new Intent(getActivity(), MainActivity.class);
startActivity(startApp);
}
});
i think you should use SharedPreferences to save a boolean flag that your first activity "the guideline" activity have been displayed .
and on your button click set the value to true like the following :
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyApplicationPref", Context.MODE_PRIVATE);
Editor editor = pref.edit();
editor.putBoolean("firstTime" , Boolean.TRUE);
editor.commit();
and in your "guideline" in your onResume method do the following :
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyApplicationPref", Context.MODE_PRIVATE);
boolean firstTime = pref.getBoolean("firstTime", false);
if(firstTime == true){
Intent myIntent = new Intent(getApplicationContext() , MainActivity.class);
startActivity(myIntent);
}
Please give me some feedback .
Hope That Helps .
im building a app, found a java part that lets me show an activity only at first start.
this works but when i kill the app or reboot my phone it goes back, can anyone help me out with this?
im using this in my main activity (Toolz.java)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Prefs.firststart == false) {
setContentView(R.layout.toolz);
} else {
Intent i = new Intent(this, First.class);
startActivity(i);
finish();
}
}
#Override
protected void onResume() {
super.onResume();
}
and i added this to First.java
Prefs.firststart=false;
and i made the Prefs.java and added this
public class Prefs {
public static boolean firststart = true;
}
you will need to use SharedPreferences instead o static variable for saving data which also available when app killed or device reboot.
you can see following tutorial how we use SharedPreferences for saving and reading data from SharedPreferences
Shared Preferences
When the app restarts all your code will start from the starting point (i.e Prefs.firststart will be set to true). Instead, you need to make a persistent variable that is saved throughout sessions. If you're using a DB you could use that, or you could use the built-in SharedPreferences, as such:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences prefs = this.getPreferences(MODE_PRIVATE);
if (prefs.contains("started")) {
setContentView(R.layout.toolz);
} else {
//Add the preference:
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("started",true);
editor.apply();
Intent i = new Intent(this, First.class);
startActivity(i);
finish();
}
}
When you kill your app, the values are going to reset to their initial values. Your boolean will be equal to true. I suggest saving the state of the variable using SharedPreferences. Check out this other question someone asked about saving.
How to save a state of a variable in android app and use it
Try it like this
//**** First start *****
//you get the prefs for your app using a pref name
String PREFS_NAME = "myAppPrefs";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
//persist the boolean value in preferences using a key-value approach
editor.putBoolean("pref_app_started", true);
//commit the changes
editor.commit();
and when you start the app you check for that pref
//**** next start *****
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
//now we get the saved boolean using the key we previously uesd when setting it
//the second parameter is the default value for the flag in case it was never set
boolean wasAppStarted = settings.getBoolean("pref_app_started", false);
//now you can use your wasAppStarted flag to decide what you do further
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.