1

I am new to Android development. I am trying to call a method of one of my classes when a button on my main activity is pressed.

On my Main Activity I have this button:

public void buttonTest(){
    Button b = (Button) findViewById(R.id.test);                
    b.setOnClickListener(new View.OnClickListener() {       
        @Override
        public void onClick(View arg0) {                    
            String s = "changeText:myText";
            Intent in = new Intent(PlusActivity.this, Test.class);  
            in.putExtra("method",s);
            startActivity(in);              
        }
    });
}

And here is is the class (without imports) which that intent above is calling to.

public class Test extends Activity {

    static String text = "test";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        TextView mTextView = (TextView) findViewById(R.id.textView);
        mTextView.setText(text);
    }

    public void changeText(String s){
        this.text = s;      
    }

    @Override
    protected void onNewIntent(Intent intent) {
       super.onNewIntent(intent);
       String[] array = intent.getStringExtra("method").split(":");
       if(array[0].equals("changeText")){
          changeText(array[1]);
       }
    }   

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.test, menu);
        return true;
    }   
}

Basically I want to know if it is possible to change the value of that String text, before onCreate(). Basically each button will have a correspondent text, and I want to be able to modify that text based on which button.

If it is, what should I do/change?

Thanks in advance.

2
  • 3
    use String value = getIntent().getStringExtra("method"); and mTextView.setText(value); Commented Feb 17, 2014 at 6:28
  • 1
    Thank you, that`s what I want. I was so obsessed in change the value of the variable I have, that I didn't thought that I could do without it. Commented Feb 17, 2014 at 6:34

3 Answers 3

5

The right way to do it is to send the string you want it to be as an extra in the intent, and to read the extra from the intent and assign it to that variable in the onCreate function.

Sign up to request clarification or add additional context in comments.

3 Comments

I am passing as an extra. And I am reading that extra on the intent creation. What should I change on my onCreate to get what the intent already have?
You mean, what @Raghunandan just commented?
in your onCreate use the code posted by raghunandan
1

Use SharedPreference. Save in OnCLick of first class and retrieve in OnCreate of second class.

Initialization

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();

Storing Data

editor.putBoolean("key_name", true); // Storing boolean - true/false
editor.putString("key_name", "string value"); // Storing string
editor.putInt("key_name", "int value"); // Storing integer
editor.putFloat("key_name", "float value"); // Storing float
editor.putLong("key_name", "long value"); // Storing long

editor.commit(); // commit changes

Retrieving Data

// returns stored preference value
// If value is not present return second param value - In this case null
pref.getString("key_name", null); // getting String
pref.getInt("key_name", null); // getting Integer
pref.getFloat("key_name", null); // getting Float
pref.getLong("key_name", null); // getting Long
pref.getBoolean("key_name", null); // getting boolean

Deleting Data

editor.remove("name"); // will delete key name
editor.remove("email"); // will delete key email
editor.commit(); // commit changes

Clearing Storage

editor.clear();
editor.commit(); // commit changes

1 Comment

Thank you for answering here, but this is quite more complex than what I want. And I already achieved what I want.
1
String text;
if (savedInstanceState == null) {
    extras = getIntent().getExtras();
    if(extras == null) {
        text= null;
    } else {
        text= extras.getString("your default string message");
    }
} else {
    String s = "your default string message";
    text= (String) savedInstanceState.getSerializable(s);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.