0

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);
}

}

Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
Oleh
  • 71
  • 1
  • 1
  • 3
  • You should probably code more clearly. Especially with logging, make the logs work for you and make them give you useful information to identify how the code is flowing. – JoxTraex Jul 22 '16 at 02:22

2 Answers2

0

When you press the button, save the string from the EditText into a SharedPreference. With that, you can retrieve that string from any Activity by calling the same SharedPreference with that key.

A quick example to Shared Preference

Community
  • 1
  • 1
0

You might try adding the second line of this to your activity in the manifest

<activity android:name=".MainActivity"
          android:launchMode= "singleInstance" >

Ref: https://stackoverflow.com/a/5784950/399741

onSaveInstanceState may not get called after onPause or onStop immediately (these methods get called when your activity loses focus or is not visible, respectively), as it is called when activity is about to be destroyed. Android normally keeps your instance variables when the activity is still alive, but merely paused or stopped.

However, Android might be creating a new instance of your activity to put on the top of the activity stack when you navigate back to it, thus making your string null. You can prevent this with the code above, ensuring the same instance is used until it is destroyed.

Edit: It might be that your instructor is just wanting you to practice passing information back and forth between activities: you could just pass the string to Activity2 in the intent's bundle, and do it again when you go back to Activity1, reseting the string in onCreate by getting the intent's bundle.

Community
  • 1
  • 1
Cartesian Theater
  • 1,920
  • 2
  • 29
  • 49