Tuesday, August 2, 2011

[android-developers] Sending intent between two activities

In my application, activity A is the default one and I have created
another activity B. There is a button in each activity. On click event
of the button, the other activity should be brought to front. So, for
the first click, the activity will be created (OnCreate will be
called) and for the subsequent clicks, new instances should not be
created but it should be brought to front only (OnNewIntent should be
called).

Button click of Activity B will do the same. But, when we click the
button on A, instead of calling OnNewIntent of B, OnCreate will be
called which means B will again be created. How can we avoid it?

public void OnMyButtonClickOfA(View view)
{
Intent i = new Intent(this, B.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP ); // What should I do here in order
to avoid the recreation of B?

i.putExtra("string", "Again Called");
startActivity(i);


}

public void OnMyButtonClickOfB(View view)
{
Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP );
//intent.putExtra("MyClass", m);
intent.putExtra("string", "Recalled");
startActivity(intent);

}

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment