it's my first questiom here. Sorry for stupidness. My assignement is to create a phone book on android. I'm so mad, frustrated and really angry,coz I have no idea how to do that.
So I decided to make very simple app at first.
Let's suppose, I have mainActivity. There is an EditText there, whrere I enter some string. Then I press a button and see Activity 2. I press a button to get back to mainActivity, and I wanna see TextView representing string inputed before. The problem is that string is null(( Before mainActivity is paused my variables, my variables are not saved? How can I save them?
Same think happen to Contact Book app. I create new contact, pass it into intent, go back to the MainActivity, but the ArrayList of contacts has only new contact. The rest of data is lost(
MainActivity :
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView txtsavedValue;
EditText edtInput;
Button btnTo2;
String string, str;
int integer1;
String LOG_TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
btnTo2 = (Button) findViewById(R.id.btnSendTo2);
btnTo2.setOnClickListener(this);
txtsavedValue = (TextView) findViewById(R.id.txtSavedValue);
edtInput = (EditText) findViewById(R.id.editTextInput);
if (savedInstanceState != null) {
string = savedInstanceState.getString("mykey");
Log.v(LOG_TAG, string);
txtsavedValue.setText(string);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View view) {
string = (edtInput.getText().toString());
Log.v(LOG_TAG, string);
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("mykey", string);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Log.v(LOG_TAG, "truuue");
string = savedInstanceState.getString("mykey");
txtsavedValue.setText(string);
}
@Override
protected void onResume() {
super.onResume();
if (string != null) {
Log.v(LOG_TAG, string);
txtsavedValue.setText(string);
} else Log.v(LOG_TAG, "nulll");
}
}
Main2Activity :
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Button b = (Button) findViewById(R.id.btnTo1);
b.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
}