I'm a beginner in Android.
I created two interfaces with DroiDraw, then I added them res/layout/a.xml and res/layout/b.xml
My goal is passing from interface a to b.
How can I do??
If you want to pass data from Activity A to ActivityB, then add it to the Intent that starts Activity B
Intent i = new Intent(this,ActivityB.class);
i.putExtra("key",value);
startActivity(i);
Also have a look at the Bundle class.
If passing means "passing data" - look into SharedPreferences or Sqlite - it depends upon your options
If Passing means "navigating" - associate an activity with interface b - so launch activity b when the user wants to navigate to it from interface a.
Related
To describe the situation, say that I have 3 activities: A, B, and C
and there is a button in activity A which starts activity B, and there is a button in activity C when it is clicked, it should send a result from activity C to activity A
My Question is...Is there a way to pass a result from C to A? If there is a way, what it is?
Note: It would be good if the way you give uses the methods startActivityForResult(...) and onActivityResult(...)
Thank you in advance
If you just using simple types like String objects you can use Bundle and supplementary variables in B,C activities. And transfer it from C->B->A using onActivity result. Or you could use Shared preferences.
There's a flag of Intent called FLAG_ACTIVITY_FORWARD_RESULT. Call:
Intent intent = new Intent(this, ActivityB.class);
startActiivtyForResult(intent);
when starting Activity B (by calling startActivityForResult(intent)).
When opening C, call:
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActiivtyForResult(intent);
finish();
which means: open C, close B. Then, when closing Acitivity C, call:
setResult(123);
finish();
the result from C will go back to A.
You can use Intent to transfer data if you are navigating from Activity C to Activity A. Otherwise, I would suggest using an Interface, and then pass the data as parameter in a callback method present in Activity A. You said you wanted answers like onActivityResult, for that I guess the Activity C should exit or something to invoke onActivityResult in the Activity A
I have Activity A which has editTexts and from activity A, a user can go to activity B and fill out more information. Once the user is done on activity B, they press a button and come back to A. I would like to come back to A with the information that the user had filled in to be already there and not have to fill in the information again.
Currently, this is my code for coming back from B to A.
Intent intent = new Intent(NewAddressActivity.this, CreditCardActivity.class);
intent.putExtra("newAddressEntered", true);
intent.putExtra("newAddress", (Serializable) newAddress);
startActivity(intent);
With this code, when the user comes back, the fields on activity A are empty.
*Note - I need to pass data from B to A when going back.
Any tips on how to do this properly would be greatly appreciated.
Thanks
You are on the right track. You can do that using .putExtra
You can pass extra data via the intent using intent.putExtra("key",
text_field.getText().toString()) on the intent before you send it (in
the first activity) and getIntent().getExtras().getString("key") in
the second activity.
This is assuming text_field is your EditText you want to pass the
value from. You can change "key" to whatever you want, too.
You're actually not "coming back", but starting a new empty intent, thats why the fields are blank again. Instead, you should just call:
finish()
Regarding filling related fields from different Activities, I wouldn't recommend that, but if you need to keep it that way, you may have a singleton class to store all field values for instance.
Also, you could send B fields to A as activity results.-
http://developer.android.com/training/basics/intents/result.html
But still, I would recommend keeping all related fields in one Activity.
Change the launchmode of your Activity A (CreditCardActivity) to singleTop in manifest file and try. The documentation says
If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.
So your activity should retain old values.
hi i have an application
A is launcher activity from that i create B activity. From B i want to go back to A without deleting the instance of B(so no finish) then i want to create a new instance of B lets call it B2. And from A i want to be able to show B or B2 without recreating them.
again i stress that B,B2 are the same class just diffrent instances.
from B when i want to go to A i thought of using
Intent myIntent = new Intent(getBaseContext(), MainAct.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(myIntent);
but i think that its a mistake because it will clear other instances.
what intent must i use in the cases?
in c# windows application we would do B.hide();A.show(). then from A we could do
A.hide(); B.SHOW() or B1.show();
how can i do that in android please?
If B is an Activity, you can't have two different instances of it.
You can have 2 different Activities that extend the same class, but you've indicated you don't want that.
You need to find a way to change the appearance and behavior of B rather than creating a new instance.
Please consider reading about the Activity Lifecycle.
use FLAG_ACTIVITY_REORDER_TO_FRONT
from the doc
If set in an Intent passed to Context.startActivity(), this flag will
cause the launched activity to be brought to the front of its task's
history stack if it is already running.
From ActivityA I'm starting ActivityB.
In ActivityB I'm creating a new Serializable object.
After the object has been created I want to close ActivityB and pass the new object to ActivityA.
How can I do it?
start Activity B with startActivityForResult().
In activity B, when the object is created create an Intent to pack the object in:
Intent result = new Intent();
result.putExtra("result", object);
setResult(RESULT_OK, result);
Then you will receive that intent in the onActivityResult() method of Activity A, where you can extract it like so:
data.getSerializableExtra("result");
Start the Activity B using startActivityForResult method.
When you finish creating object call setResult in Activity B. Set Your Data in Intent. You don't need to finish this.
Override function onActivityResult in Activity A. This will be called when you call setResult in Activity B. You can receive the data from Intent passed from Activity B.
But most of the time, you need separate Activities if only you have different screens with different tasks. Otherwise accomplish the task within the same Activity. *(A Good and Standard Practice).*
after the object is created, create an intent object, put that object to that intent and then start activity A. In the Activity A's onRestart() get that intent and from that intent get that object.
Why are you doing that? If you're using activity B only for creating new object, you can do it in a plain simple java class. What are you trying to accomplish?
I want to pass an object from Activity B to Activity A.
Scenario:
- Activity A calls Activity B
- User selects item in Activity B
- Activity B passes an object to Activity A
How do I accomplish this? And in which method do I read in the passed object in Activity A?
you can start your intent using startActivityForResult on Activity A ... And when finishing Activity B declare a bundle and put your serializable object to your bundle and add it to your intent. On Activity A's onActivityResult method you can get this intent back and retrieve your bundle...
Refer to this sample below.
http://micropilot.tistory.com/1577
Intents are used to send data between 2 activities....If its serializable data than send it using:
Eg:
Intent myIntent = new Intent(home.this,dvd.class);
myIntent.putExtra(name, value);