I'm trying to send a madeObject from Start_Activity to Next_Activity after finishing Start_Activity.
How to make a code in Start_Activity and Next_Activity?
My sequence is :
1) Start_Activity.onCreate()
2) Start_Activity.makeObjectData()
3) Start_Activity.putextras(madeObject)
3) Start_Activity.startActivity() : start Next_Activity.
4) Start_Activity.finish() : finish Start_Activity
5) Next_Activity.getExtras()
Here is Start_Activity
public class Start_Activity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_launching_activity);
:
//// makeDataObject ///
:
Intent intent = new Intent(getApplicationContext(), Next_Activity.class);
Bundle bundle = new Bundle();
bundle.putSerializable(madeDataObject);
intent.putExtras(bundle);
startActivity(intent);
finish();
}
}
This is Next_Activity
public class Next_Activity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a_launching_activity);
??? <-- How to get the madeDataObject of Start_Activity?
}
}
bundle.putSerializable takes two parameters. The first is a String. It has to be unique for this bundle object. You will use it afterwards, to retrieve your object.
bundle.putSerializable("my_unique_key", madeDataObject);
intent.putExtras(bundle);
startActivity(intent);
on Next_Activity, you can retrieve the intent with getIntent()
Intent intent = getIntent();
if (intent.getExtras() != null) {
intent.getExtras().getSerializable("my_unique_key");
}
Of course the object you are trying to pass from one activity to another has to implements Serializable
send extras to next activity as:
Intent intent = new Intent(getApplicationContext(), Next_Activity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("myclass", madeDataObject);
intent.putExtras(bundle);
startActivity(intent);
finish();
Get it in next activity like:
Intent intent = getIntent();
if (intent.getExtras() != null) {
intent.getExtras().getSerializable("myclass");
}
Related
I have an unexpected crash on my app in production. I have an activity that I always start using a newInstance method that add some extras to the intent :
public static Intent newInstance(final Context context, #NonNull final Location location) {
final Intent intent = new Intent(context, LocationActivity.class);
final Bundle extras = new Bundle();
extras.putParcelable(BUNDLE_ITEM_LOCATION, Location);
intent.putExtras(extras);
return intent;
}
And here is my onCreate method :
#Override
#CallSuper
public void onCreate(#Nullable final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Bundle extras = getIntent().getExtras();
if (extras == null || !extras.containsKey(BUNDLE_ITEM_LOCATION)) {
throw new IllegalArgumentException(LocationActivity.class.getName()
+ " must be created by using newInstance()");
}
[... Some other code ...]
}
And this IllegalArgumentException is thrown in production. It's super weird because this case should never happen. Any idea about the cause?
Thanks in advance.
Try this:
public static Intent newInstance(final Context context, #NonNull final Location location {
final Intent intent = new Intent(context, LocationActivity.class);
intent.putExtras(BUNDLE_ITEM_LOCATION, Location);
return intent;
}
I have two activities: A and B.
In activity A: On "btn_navSimilarColor" buttonClick - I made a call to B with startActivityForResult. There are already some intents inside A to use camera and gallery and a intent data i am receiving from previous activity.
In activity B: I made a asyncTask call inside onCreate() and in asyncTask's onPostExecute() i am sending back intent extra to Activity A.
Activity A:
public class A extends Activity
{
...
#Override
public void onCreate(Bundle savedInstanceState)
{
...
Bundle extras = getIntent().getExtras();
if (extras != null) {
edtTxtColorCode.setText(extras.getString("xtra_selectedColor"));
} else {
Toast.makeText(this, "There was a problem in the response!", Toast.LENGTH_SHORT).show();
}
}
public void buttonOnClick(View view)
{
switch (view.getId())
{
case R.id.btnCamera:
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), FLAG_CAMERA);
break;
case R.id.btnGallery:
startActivityForResult(
new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI), FLAG_GALLERY);
break;
case R.id.btn_navSimilarColor:
Intent intnt_similar = new Intent(A.this, B.class);
intnt_similar.putExtra("xtraColor", edtTxtColorCode.getText().toString());
startActivityForResult(intnt_similar, FLAG_navSimilarColorAct);
break;
default:
break;
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v("resultCode","="+resultCode);
if (resultCode == Activity.RESULT_OK)
{
mCursor = null;
if (requestCode == FLAG_GALLERY)
onSelectFromGalleryResult(data);
else if (requestCode == FLAG_CAMERA)
onCaptureImageResult(data);
else if(requestCode == FLAG_navSimilarColorAct)
{ Bundle extras = getIntent().getExtras();
String stt = extras.getString("intnt_similarColor");
if (extras != null)
edtTxtColorCode.setText(extras.getString("intnt_similarColor"));
}
}
}
}
Activity B:
public class B extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
....
receiveIntent();
new AsyncConver().execute();
}
private void receiveIntent() {
Bundle extras = getIntent().getExtras();
if (extras != null)
strIntentrecvdColor = extras.getString("xtraColor");
else
Toast.makeText(this, "There was a problem in the response!", Toast.LENGTH_SHORT).show();
}
class AsyncConvert extends AsyncTask<String, Integer, String>
{
...
#Override
protected void onPostExecute(String s)
{
super.onPostExecute(s);
Custom_SimilarColorListAdapter gridAdapter = new Custom_SimilarColorListAdapter(SimilarColors.this, list_SimilarColors);
grdVw.setAdapter(gridAdapter);
grdVw.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
String str_colorCodeSimilar = ((TextView) v.findViewById(R.id.listrow_similar_code)).getText().toString();
Toast.makeText(getApplicationContext(), "ID:: "+ str_colorCodeSimilar , Toast.LENGTH_SHORT).show();
Intent retrnIntnt = new Intent();
retrnIntnt.putExtra("intnt_similarColor", str_colorCodeSimilar);
setResult(RESULT_OK, retrnIntnt);
finish();
}
});
}
}
}
Problem:
Now the problem is that i am getting data in activity B - as i am already checking it with
Toast.makeText(getApplicationContext(), "ID:: "+ str_colorCodeSimilar , Toast.LENGTH_SHORT).show();
But in Activity A's onActivityResult i am not getting the bundle extra data for "intnt_similarColor" which is:
String stt = extras.getString("intnt_similarColor");
instead i am getting bundle extra for "xtra_selectedColor" which is inside onCreate().
Why is this happening and how am i getting the previous bundle data , not the one that has been passed from activity B?
change Bundle extras = getIntent().getExtras(); to Bundle extras = data.getExtras();
Get string from data intent that you receive from onActivityResult. You use Bundle extras = getIntent().getExtras(); where getIntent() is actually class A's received intent.
So you have to use:
String stt = data.getStringExtra("intnt_similarColor");
I have 3 activities : activity "valider" as 1st activity and the second is activivty "connexion" and finally the inscription activity , i hope to pass from 1st==> second==> third and viceversa from third==> second==>first activity.
Please someone can help me?
There are multiple ways to share data with different activities your question is repeated please check this: What's the best way to share data between activities?
To pass data in the First:
void send() {
Intent intent = new Intent(this, Second.class);
intent.putExtra("NAME",some_value);//your data
startActivityForResult(intent, yourSomeId);// if you want to retrieve callback data later
startActivity(intent);//or simply
}
in the Second
in
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if(extras == null) {
yourData = null;
} else {
yourData = extras.getString("NAME");
}
}
to send data back:
charge data in the second activity:
void back() {
Intent intent = new Intent();
intent.putExtra("NAME", yourdata);
setResult(RESULT_OK, intent);
finish();
}
and retrieve in the First:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null) {return;}
yourdata = data.getStringExtra("NAME");
}
you can send the data to the third activity in the same way.
I am building an Android app. How can I get the calculations' solutions to be displayed on a new activity?
Make your calculation in Activity A.
And send them to the other activity using intent.putExtra(...)
Take a look in this guide.
On activity A
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("YourKeyWord", yourVariableWithValue);
startAcvitity(intent);
On activity B
int result;
Bundle extras = getIntent.getExtras();
if (extras =! null) {
result = extras.getInt("YourKeyWord");
}
Activity A:
public class First extends Activity{
int a,b,c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
a = 1,b = 2;
c = a + b;
Intent intent = new Intent(this,Second.class);
intent.putExtra("result", c);
startActivity(intent);
}
}
Activity B:
public class Second extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
int result = intent.getExtras().getInt("result");
}
}
I'm currently writting an app for Android. I'm trying to figure out the best place to catch on intent when the activity is created for the first time.
public class DisplayAct extends Activity {
private Intent mNewIntent;
private ViewFinderFragment mViewFinderFrag;
public boolean toot;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
setContentView(R.layout.activity_display);
if(savedInstanceState != null) {
Intent intent = getIntent();
if (intent != null) {
String action = intent.getAction();
}
}
This is how I get the intent.
I was wondering if grabbing the intent at oncreate is good or it's better to add onStart and add this action inside in term of design perspective.
Thanks
If you app is open, handle it with onNewIntent. If you app is closed, you wanna use your Bundle that comes with your Intent.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState.getString(MY_ACTION)) {
// Do something
}
EDIT:
I set this action as a String to the Intent's Bundle. I performed it with the code below:
Bundle bundle = new Bundle();
bundle.putString(MainActivity.MY_ACTION, "MY_ACTION");
Intent intent = new Intent(this, MainActivity.class);
intent.putExtras(bundle);