Android: why use intent.putExtra() method? -
Android: why use intent.putExtra() method? -
this real noob question i'm sure, finding quite perplexing.
why earth want ever utilize intent.putextra method share info between classes in android?
let me explain. making first android app next instructions developers guide (i @ moderate level java) , using code looks this:
//class field
//key holds string????? not understanding this... public static final string extra_message = "self.anon.myfirstapp.message"; //this method activated button beingness pressed public void sendmessage(view view) { intent intent = new intent(this,displaymessageactivity.class); edittext edittext = (edittext) findviewbyid(r.id.edit_message); string message = edittext.gettext().tostring(); //puts string message within string extra_message - why???? intent.putextra(extra_message, message); startactivity(intent); }
ok firstly want point out see happening , part how works (am confused field declaration = "myclasspath" why?)...
but....
surely easier have static field called:
public static string message;
then method this:
public void sendmessage(view view) { intent intent = new intent(this,displaymessageactivity.class); edittext edittext = (edittext) findviewbyid(r.id.edit_message); message = edittext.gettext().tostring(); startactivity(intent); }
then when class displaymessageactivity needs string message calls for:
string message = myclass.message;
that seems much more straight forward. creation of new string extra_message seems hold string message , why send intent when other class can access info straight anyway -- , field declaration "self.anon.myfirstapp.message" mean? can find no such folder or path leading anything.
as else stated there situations (such screen rotate) in android scheme destroys , restarts app - variable info lost. work consistently way suggest if info hard coded final variable. not reason using intents though.
the great thing using intent pass info can utilize intent not communicate sub-activities within own application activity installed on android system. illustration may want launch intent starts phone application , include number want call.
perhaps improve question yours though "why not utilize intents pass information?" intent.putextra()
method allows convenient flexible , straight forwards method pass much info in safe , secure way other activity.
android android-intent
Comments
Post a Comment