0

I have a code to change the text of textview in a layout according to the value of the edittext (et) in the previous layout

there is MorningDrsGeneral :

 public class MorningDrsGeneral extends ActionBarActivity {
 Button button ;
 EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.morningdrs);


    et = (EditText) findViewById(R.id.et);
    addListenerOnButton1();
  }
  public void addListenerOnButton1() {

        final Context context = this;

        button = (Button) findViewById(R.id.button1);

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {


          Intent intent = new Intent(context, bookingKamal.class);
                intent.putExtra("fn" , et.getText().toString());
                startActivity(intent);}


        });}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

and there is bookingKamal.java :

 public class bookingKamal extends ActionBarActivity {
Button button ;
 TextView textView3 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bookingkamal);

   textView3 = (TextView) findViewById(R.id.textView3) ;
   String A = textView3.getText().toString();
   String N = " " ; 
   if (A.equals(N)){
    Intent  intent = getIntent(); 
   String texx = intent.getStringExtra("fn") ; 
    textView3.setText(texx);
   }}








@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
   }

I have to keep the text in the bookingkamal layout .

It means when I go back from this layout and back to it the text should be the same as previous.

Jonny C
  • 1,943
  • 3
  • 20
  • 36

2 Answers2

0

Then, it should be like this

bookingKamal.java

String texx;
void onBackPressed() {
         Intent intent = new Intent(getApplicationContext(), MorningDrsGeneral.class);
         intent.putExtra("texx" , texx);
         startActivity(intent);
}

MorningDrsGeneral.java

    protected void onCreate(Bundle savedInstanceState) {
        et = (EditText) findViewById(R.id.et);
        Intent intent2 = getIntent();
        if (intent2.getStringExtra("texx") != "") {
               String abcd = intent2.getStringExtra("texx");
               et.setText(abcd); 
        } 

   }
andika_kurniawan
  • 511
  • 4
  • 9
  • 20
  • This is not the right way to do it.. It's better to use SharedPreferences. This will save the data (even if you close your application => if you want this of course). The method you are using is not conveniently. When you put a new fragment between those 2, then you are forced to change your codes everywhere. It's better to save the data wit sharedPreferences – Nike Sprite May 09 '15 at 22:01
  • You are right. If using fragment, this manner willn't work. I think we must create class that maintenances SharedPreferences. – andika_kurniawan May 12 '15 at 04:19
0

You can use SharedPreferences, this will save everything if you leave the layout or even (if you want) if you close your app. Take a look at the Android documentation at http://developer.android.com/reference/android/content/SharedPreferences.html

Nike Sprite
  • 476
  • 2
  • 16