Keeping inputs in activity after moving to another activity - android

I am using Android Studio. I have two activities, MainActivity and Main2Activity. There is an edit text and a button in each one. How do I keep the input in the edit text in any activity when I go to the second activity? I tried many answers but nothing worked.
Here is the code of the activity.
public class MainActivity extends AppCompatActivity {
EditText editText2;
Button button;
String var1 ;
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("var1", var1);
editText2 = (EditText)findViewById(R.id.editText2);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
var1 = savedInstanceState.getString("var1");
var1 = editText2.getText().toString();
editText2.setText(var1);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Click1(View view)
{
Intent i = new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
}

To carry over values from one activity to another, you need to add a line of code before startActivity(i);. This line of code that you need to add is:
i.putExtra("inputValue", String.valueOf(editText2.getText()));
Then in the other activity, in the onCreate() method, add this:
Intent intent = getIntent();
String inputValue = intent.getStringExtra("inputValue");
editText.setText(inputValue);
Obviously I don't know the name of your EditText in the MainActivity2 so replace the editText with its name.
Hope this helps!

You need to check for the existence of savedInstanceState in your onCreate function.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check for a savedInstanceState
if(savedInstanceState != null){
var1 = savedInstanceState.getString("var1");
editText2.setText(var1); // Making sure that you have assigned editText2 already, of course.
}
}

Related

Android Intents to pass data

I don't know why, but this isn't working. Can anyone see anything wrong with my code.
I have two activities and I'm passing data from two
public class InputActivity extends AppCompatActivity {
EditText number1EditText;
EditText number2EditText;
Button addButton;
InputActivity code looks like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
number1EditText = (EditText)findViewById(R.id.editText);
number2EditText = (EditText)findViewById(R.id.editText2);
addButton = (Button)findViewById(R.id.button);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent mIntent = new Intent(getApplicationContext(), AddActivity.class);
mIntent.putExtra("number1", number1EditText.getText().toString());
mIntent.putExtra("number2", number2EditText.getText().toString());
startActivity(mIntent);
}
});
}
AddActivity code looks like this
public class AddActivity extends AppCompatActivity {
TextView answer;
double y=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
answer =(TextView)findViewById(R.id.textView);
String value1 = getIntent().getExtras().getString("number1");
String value2 = getIntent().getExtras().getString("number2");
answer.setText((int) (Double.parseDouble(value1)+Double.parseDouble(value2)));
}
Just looking quickly:
answer.setText((int) (Double.parseDouble(value1)+Double.parseDouble(value2)));
You're passing a int to the method setText() which happens to have an overload that receives a int argument for the cases when you pass the reference from a String in some xml. You may be getting a ResourceNotFoundException.
If you want to show a text with the sum between your values:
answer.setText(String.valueOf((int) (Double.parseDouble(value1)+Double.parseDouble(value2))));
Just keep in mind that there are a lot of checks you have to do first, you may get another exceptions doing this kind of parse, like NumberFormatException for instance.

AsnycTask Not Working

EDIT
Okay really simplified version, let's call this my Activity2
public class test {
String username, id;
TextView view1, view2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
view1 = (TextView)findViewById(R.id.textView1);
view2 = (TextView)findViewById(R.id.textView2);
new testFunction().execute();
view1.setText(username);
view2.setText(id);
}
private class testFunction extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... x) {
username = "1";
id = "2";
return null;
}
}
}
Activity1 has a submit button that starts Activity2 (this activity). After Activity2 calls onCreate(), "12" will appear on the screen. When I click back (destroy Activity2 and go back to Activity1), AND THEN go back to Activity2 again, nothing appears on the screen. Why is this?
This is one problem
new testFunction().execute("1", "2").get();
You should not use get(). It blocks the ui thread waiting for the result. And you should never block the ui thread. Use
new testFunction().execute("1", "2");
Declare the variables as instance variables
TextView view1,view2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data);
view1 = (TextView)findViewById(R.id.textView1);
view2 = (TextView)findViewById(R.id.textView2);
new testFunction().execute("1", "2")
Then in Asynctask onPostExecute update the views
#override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
// update view here
}
finish the actvity when you have pressed back button like
Intent i = new Intent(this,Here is your class name.Class);
startActivity(i);
finish();
by this whenever you will go in second act by press submit button that time on create call
and you can also call your asnyc task in on resume method every time on resume will call.
You have to store the username and id value when closing the fragment.
Have a look into Fragment lifecycle onSaveInstanceState.
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(ARG_NAME, username);
outState.putBoolean(ARG_ID, id);
I have add some code lines to your onCreate method
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_data);
//get the values from previous avtivity
Bundle bundle=getIntent().getExtras();
username=bundle.getString("username").toString();
id=bundle.getString("id").toString();
String result = new testFunction().execute("1", "2").get();
TextView view1 = (TextView)findViewById(R.id.textView1);
TextView view2 = (TextView)findViewById(R.id.textView2);
view1.setText(username);
view2.setText(id);
}
in order to get the values you must pass the values from previous activity, I guess you have did it correctly, what you missed is, you forgot to get the Intent values and assign them to username,id variables

Want to Run a function without click any button when application start

I have a function named 'func()'. I want to run this function when application start without clicking any button. just when application load I want to show a massage.that massage in that function. I just want to run that function when app start what will be the code.
public class TextViewActivity extends Activity {
public static EditText etxt;
public final void func(){
etxt.setText("Massage");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
}
}
Just put a call to the function on the onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
func(); //A call to the function.
}
Hope that helps.
I don't recommend subclassing the Application in order to do this. When the application starts it will go to the main activity. So I would say just keep a SharedPreference boolean value if it has been set. If not show the message.
So keep state of the application here: http://developer.android.com/reference/android/content/SharedPreferences.html, just set a boolean. Remember when you app gets called, the activity onCreate always gets called of the main activity, so its just a matter of not calling it again.
try using below code.. you need to call ur function after you initialize edittext etxt. so it can not cause you NPE
public final void func(){
etxt.setText("Message");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
etxt= (EditText) findViewById(R.id.etxt2);
func();// here your function call.
}
Try this:
public class TextViewActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.etxt2).setText("SMTH");
}
}
In case you really need a function to be called, you can use this:
public class TextViewActivity extends Activity {
public final void func() {
findViewById(R.id.etxt2).setText("SMTH");
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
func();
}
}

Rotating device loosing data

#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
String phonenumber = phoneNumberLogin.getText().toString();
savedInstanceState.putString("PhoneNumber", phonenumber);
super.onSaveInstanceState(savedInstanceState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
String phonenumber= savedInstanceState.getString("PhoneNumber");
if (phonenumber != null) {
EditText FirstName = (EditText) findViewById(R.id.editTextname);
FirstName.setText(phonenumber);
}
super.onRestoreInstanceState(savedInstanceState);
}
I have one editText (for typing phone number). When in portrait mode I type the numbers and after that rotating the phones, the typed data is gone. I save data savedInstanceState but I have no idea how can I restore my typed data when I rotate the phone. Any help please.
On simple way is to avoid having your activity recreated on rotation. Change your AndroidManifest.xml to include android:configChanges="keyboardHidden|orientation" like this:
<activity android:name="MyActivity" android:configChanges="keyboardHidden|orientation"></activity>
Your layout will need to support whatever orientation the device supports. Also there are some other things to consider: Why not use always android:configChanges="keyboardHidden|orientation"?
try to move getting phone number from onRestoreInstanceState to onCreate method
or try something like this:
#Override
public Object onRetainNonConfigurationInstance()
{
return phoneNumberLogin.getText().toString();
}
and in onCreate
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
String phonenumber= (String)getLastNonConfigurationInstance();
if (phonenumber != null) {
EditText FirstName = (EditText) findViewById(R.id.editTextname);
FirstName.setText(phonenumber);
}
}

How to call function of one activity from another activity

Actually i want to call a function of one activity from another activity..i write a simple code for that but its not working..and show runtime error..please check the code...if there any mistake..
code for activity1:
public class Activity1 extends Activity2
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
call();
}
public void call()
{
showToast("Helloo");
}
}
code for activity2:
public class Activity2 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showToast(String s)
{
EditText t=(EditText)findViewById(R.id.editText1);
t.setText(s);
}
}
Your problem is that you're calling findViewById on a view that doesn't exist.
Activity1 is extending Activity2.
You call the super.onCreate in Activity1 which calls the onCreate in Activity2 which calls setContentView() for R.layout.main.
I'm guessing your text R.id.editText1 is in the main layout.
When Activity1 returns from the call to super.onCreate it immediately resets the content layout to main2.
The edit text box that you are trying to edit no longer exists. findViewById can not find it because the layout is not active. Thus it crashes.
To fix it try this:
public class Activity1 extends Activity2
{
private EditText et;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
et = (EditText) findViewById(R.id.editText2);
call();
}
public void call()
{
showToast("Helloo", et);
}
}
Where R.id.editText2 is an edit text box in the layout main2.
In Activity2:
public class Activity2 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showToast(String s, EditText t)
{
t.setText(s);
}
}
First, this is a bad design principle since only one Activity is active at a time. You can make a method static and then you can cross call them but at that point it should be in some sort of common util class.
The simplest way is to declare your showToast() method as public static, this way you can call it without having an instance of Activity2.
if you put it in as static you should declare it on your main activity

Categories

Resources