1

I've created three activities the first one is the Main_Activity, the second is called new_button, and the third is called general_button. The main_activity contains the other two activities, with the button (New) for the new_button activity, and the button (general) for the general_button activity. the new_button activity contains one EditText and one button called done_button. the general_button activity contains only one TextView. I used Intent to transfer the written data in the EditText from the new_button activity to the TextView in the general_button activity with the following code:

new_button(Activity):

package com.tarek.topshine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class new_button extends Activity {

Button done_button;
String text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_button);
    addListenerOnButton();}

    public void addListenerOnButton() {

        done_button = (Button) findViewById(R.id.done_button);
        done_button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {

            EditText editText = (EditText)findViewById(R.id.editText1);
            String text = editText.getText().toString();

                 Intent myIntent = new Intent(view.getContext(),general_button.class);
                 myIntent.putExtra("mytext",text);
                 startActivity(myIntent);

            }
        });
    }

general_button(Activity):

package com.tarek.topshine;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class general_button extends Activity {

TextView mTextview;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.general_button);

    mTextview = (TextView)findViewById(R.id.textView1);

    mTextview.setText(getIntent().getStringExtra("mytext"));
}

}

Now the problem I'm facing is that I can't save the intented information from the new_button activity to the general_button activity. for example: I need to open the general_button activity later from the main_activity and see the edited information. I read on the internet that I can use the onSavedInstanceState method, but I couldn't figure it out :(

Am I doing something wrong? and if I am, how can I solve this ?!

Thanks for the help :)

Flávio Faria
  • 6,370
  • 3
  • 37
  • 57
Tarek-Dev
  • 170
  • 1
  • 3
  • 18
  • 1
    Just a side-note: Classnames should start with Capital letters. so your `general_button` should be `GeneralButton` or maybe even better `GeneralButtonActivity`. – donfuxx Feb 17 '14 at 21:04
  • possible duplicate of [onSaveInstanceState () and onRestoreInstanceState ()](http://stackoverflow.com/questions/4096169/onsaveinstancestate-and-onrestoreinstancestate) – Merlevede Feb 17 '14 at 21:06

1 Answers1

2

You, probably, will be better off if you use PreferenceManager.getDefaultSharedPreferences()

You can read on that in http://developer.android.com/guide/topics/data/data-storage.html#pref One activity writes to the preference another one reads from the same preference.

Saving/restoring state is probably of no help because here you have shared data, not recreating the same data.

Alexander Kulyakhtin
  • 45,879
  • 35
  • 103
  • 155