-1

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 >> somethig like this

I cannot comment, so i will help you editing. Use SharedPreferences to store the string from the edittext in order to get persistance. Then get the string from the SharedPreferences and do the same thing that you are doing(pass it through the intent)

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

So i have to keep the text in the bookingKamal activity .. it means when i go back from this layout and back to it the text should be the same as previous. And in this code it back to null :/ or

Ownage
  • 35
  • 1
  • 6

2 Answers2

0

You have two options here:

  1. Save the value into the internal file system so it can still will be loaded after the app has been unloaded
  2. Save the value in the Activity which holds the button to this activity and save the text over there in a variable and when the bookingkamal activity starts, it could be read from somewhere (hint: Intent#putExtra("KEY", "VALUE"))

I am not describing how you should do it because there is enough documentation about it out there and that is not the point of this answer. This should give you an idea how to it.

engineercoding
  • 832
  • 6
  • 14
  • its not a constant value . it is a value which user enter it can you tell me how to do what u say ?? – Dua'a Shloul May 09 '15 at 18:03
  • Look up intents to start activities. You also should set the variable from bookingKamal from the activity which starts this activity. I want to help you, but I am not going to write the full code for you. – engineercoding May 09 '15 at 18:06
-1

You can do that by using sharedpreferences.

The above link will help you undderstand how to use it.

To add data to SharedPreferences use

 SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
 editor.putString("text", mSaved.getText().toString());
 editor.putInt("selection-start", mSaved.getSelectionStart());
 editor.putInt("selection-end", mSaved.getSelectionEnd());
 editor.apply();

And to retrieve

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) 
{
  //mSaved.setText(restoredText, TextView.BufferType.EDITABLE);
  int selectionStart = prefs.getInt("selection-start", -1);
  int selectionEnd = prefs.getInt("selection-end", -1);
  /*if (selectionStart != -1 && selectionEnd != -1)
  {
     mSaved.setSelection(selectionStart, selectionEnd);
  }*/
}

Note : that SharedPreferences will save the value such that, even of you close the application and restart it. The values will still remain. So, make sure you clear the data when you done with it. You can read more about it here at the SharedPreferences docs.

Community
  • 1
  • 1
Kushal Sharma
  • 5,978
  • 5
  • 25
  • 41
  • This should not be the answer because it can be done more easily with intents and a simple variable in the parent activity. This way it is ensured the value is not saved when one closes the app and you don't have to worry about clearing the data. – engineercoding May 09 '15 at 18:58
  • @engineercoding it is one of the ways to solve the problem. Yes there are other ways also! and it's no reason to downvote :( Also the question talks about SharedPreferences, so i thought i will point out the same. have a nice day :) – Kushal Sharma May 09 '15 at 19:06