i have to activities AnswerQuestion.java and SendAnswerToServer.java, and i want to send data from first activity to another one
on the AnswerQuestion activity i write this:
Bundle basket = new Bundle();
basket.putString("time", timeToAnswer+"");
Intent goToSendServer = new Intent(AnswerQuestion.this, SendAnswerToServer.class);
goToSendServer.putExtras(basket);
startActivity(goToSendServer);
my question what have i to write on the SendAnswerToServer activity , thank you
in SendAnswerToServer Activity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getIntent().getExtras();
if(bundle !=null)
{
//ObtainBundleData in the object
String strdata = bundle.getString("time");
//Do something here if data received
}
else
{
//Do something here if data not received
}
Related
I have two classes. class A and class B. In class A, I put some strings into a bundle object and then send it to class B.
My question is here:
Can I put some string or int to this bundle directly without extracting it in class B and add extra info to it and then create a new Bundle.
*******EDIT*******
other scenario:
I have sent a bundle from intent service to activity then in activity I want to put some extra to this bundle and finally I'll send this bundle to background thread for saving on sharePreference. how could i do that?
Yes you can do that. So in your Class B, when you want to open Class C, you can get the bundle from previous intent and then go ahead and add to the same bundle and open C
Intent newIntent = new Intent(ActivityB.this, ActivityC.class);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
bundle.putString("newKey","newValue");
newIntent.putExtras(bundle);
}
startActivity(newIntent);
Update
since you want to pass this Bundle in BG thread and assuming you are using AsyncTask for that you can do following -
Declare your Async task as below -
class MyBGClass extends AsyncTask<Bundle,Void,Void>{
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Void doInBackground(Bundle... params) {
return null;
}
}
Then to call it you can do -
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
bundle.putString("newKey","newValue");
}
new MyBGClass().execute(bundle);
UPDATE 2:
To pass data from your resultReceiver -
receiver.send(RESULT_CODE, bundle);
Then in your activity where you would have passed this resultreceiver to your Service implement
#Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
//resultData is your bundle. to add to it -
if (resultData != null) {
resultData.putString("newKey","newValue");
}
}
Then call your BG thread.
Change the constructer of your WorkerThread class like below and pass bundle to your thread when you create the object of the class-
private class WorkerThread extends HandlerThread {
private static final int SAVE = 1;
PreferenceModel instancePreferenceModel;
Bundle bundle;
private Handler mHandler;
public WorkerThread(Bundle bundle) {
super("WorkerThread", Process.THREAD_PRIORITY_BACKGROUND);
App app = (App) getApplication();
this.bundle = bundle;
instancePreferenceModel = app.getInstancePreferenceModel();
}
#Override
protected void onLooperPrepared() {
super.onLooperPrepared();
mHandler = new Handler(getLooper()) {
#Override // receive message from uiThread as Bundle object.
public void handleMessage(Message msg) {
switch (msg.what) {
case SAVE:
Log.i(TAG, "handleMessage: saved completed");
Bundle data = (Bundle) msg.obj;
Log.i(TAG, "getData: " + data.getString(LoginIntentService.EXTRA_FIRST_NAME));
instancePreferenceModel.saveUserInfo(data);
break;
}
}
};
}
// this method is access to main ui thread.by this method UiThread can send a Message
// Background Thread
public void write(Bundle b) {
Message msg = new Message();
msg.obj = b;
Message obtain = Message.obtain(mHandler, SAVE, msg.obj);
mHandler.sendMessage(obtain);
}
}
I have some class.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent newintent = new Intent(getApplicationContext(),Main2Activity.class);
newintent.putExtra("SOME_Data", "Perfect_Data");
startActivity(newintent);
finish();
}
}
I want to retrive some data in another class.
public class Main2Activity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Intent i = this.getIntent();
Bundle s = getIntent().getExtras();
}
}
But when i launch this code,instead bundle with string "Perfect_Data"
I get this error "Bundle[mParcelledData.dataSize=68]".
Can you help me?
If you want to print the content of Bundle, use this:
for (String key : bundle.keySet()) {
Object value = bundle.get(key);
Log.d(TAG, String.format("%s %s (%s)", key,
value.toString(), value.getClass().getName()));
}
Otherwise it just uses the default toString() of the Object class, which is what you got.
The Bundle gets printed using the default toString function.
If you just want to get the String, change it to this:
Intent i = this.getIntent();
String data = i.getStringExtra("SOME_Data");
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);
Hi I have a Requirement like Person Details for that i have multiple details like personal,educational,job like In first activity am enter personal details and then educational and then job like that one after another for that how can i maintain all details in single object and pass through app please help me.
Be aware that you need to bundle an object in an Intent, that object must implement the Parcelable interface. Here's a common implementation taken from the Parcelable documentation...
public class MyParcelable implements Parcelable {
private int mData;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(mData);
}
public static final Parcelable.Creator<MyParcelable> CREATOR
= new Parcelable.Creator<MyParcelable>() {
public MyParcelable createFromParcel(Parcel in) {
return new MyParcelable(in);
}
public MyParcelable[] newArray(int size) {
return new MyParcelable[size];
}
};
private MyParcelable(Parcel in) {
mData = in.readInt();
}
}
Once you've done that, simply pass the object around in an Intent...
Intent i = new Intent(getApplicationContext(), ACTIVITY_TO_START.class);
i.putExtra("extra_key", new MyParcelable());
startActivity(i);
to retrieve the object for the starting activity...
Bundle extras = getIntent().getExtras();
if(extras != null && extras.containsKey("extra_key"))
{
MyParcelable p = (MyParecelable)extras.getParcelable("extra_key");
}
Simple solution: Use intents for this
Personal.class
public class Personal extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personal);
Intent i = new Intent(getApplicationContext(), Educational.class);
i.putExtra("personal_details",<-get data from object->);
startActivity(i);
}
}
Educational.class
public class Personal extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personal);
String personal_details;
Bundle extras = getIntent().getExtras();
if (extras != null) {
personal_details= extras.getString("personal_details");
}
Intent i = new Intent(getApplicationContext(), educational.class);
i.putExtra("personal_details",personal_details);
i.putExtra("educational_details",<-get data from object->);
startActivity(i);
}
}
Job.class
public class Personal extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personal);
String personal_details,educational_details;
Bundle extras = getIntent().getExtras();
if (extras != null) {
personal_details= extras.getString("personal_details");
educational_details= extras.getString("educational_details");
}
Intent i = new Intent(getApplicationContext(), FinalResult.class);
i.putExtra("personal_details",personal_details);
i.putExtra("educational_details",educational_details);
i.putExtra("job_details",<-get data from object->);
startActivity(i);
}
}
FinalResult.class
public class Personal extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.personal);
String personal_details,educational_details,job_details;
Bundle extras = getIntent().getExtras();
if (extras != null) {
personal_details= extras.getString("personal_details");
educational_details= extras.getString("educational_details");
job_details= extras.getString("job_details");
}
}
}
{EDIT-1}
have a look at one of my answers in different storage options in
android - Click Here
You can achieve it using application variable or shared
preferences but i would not recommend it. !
Try storing it in POJO(Plain old Java class)
{EDIT-2}
Hmm if i understand correctly your question:: You are connected to internet(Wifi,Wired,... etc) basically you are trying to show a dialog when there is no network connectivity ! .... You can take the help of Broadcast receivers ...
Try this:: Set the broadcast receiver to fire the intent when there is no net connectivity ....
Write the code to catch that intent and pop the dialog .... In this dialog give the user option to reconnect the connectivity !
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");
}