How can i pass onActivityResult data in activity? - android

#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 900) {
if (resultCode == getActivity().RESULT_OK) {
Bundle b=data.getExtras();
if(b!=null){
Playlist playlist = (Playlist) b.getSerializable("obj");
int playlistId = data.getIntExtra("PLAYLIST_ID", 0);
Log.d("---->Data ID", String.valueOf(playlistId));
}
}
}
How can I send that playlistId Value in onCreate() method?

ResultActivity:
intent.putExtra("yourKeyName", "hello");
setResult(900, intent);
Get the result:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode== 900) {
if (resultCode == getActivity().RESULT_OK) {
String hello = data.getStringExtra("yourKeyName");
}
}
You dont have to create a new Bundle, just get extra content from the "Intent data".
Hope this helps.

#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode== 900) {
if (resultCode == getActivity().RESULT_OK) {
Bundle b=data.getExtras();
if(b!=null){
Playlist playlist = (Playlist) b.getSerializable("obj");
int playlistId = data.getIntExtra("PLAYLIST_ID", 0);
Log.d("---->Data ID", String.valueOf(playlistId));
}
}
}
use this in another activity to get the result back
Intent intent = new Intent();
intent.putExtra("key",value);
setResult(900,intent );

Related

Use onActivityResult when button IsClicked

I have an activity that once a ToggleButton is clicked, it starts another activity as follows:
Intent intent = new Intent(ChatActivity.this, BBActivity.class);
startActivityForResult(intent,1);
In that second activity it performs few calculations and then:
confirmedData.putExtra( "confirmedB", (Serializable) bSelected );
confirmedData.putExtra( "dateInMillis",DateInMillis );
setResult(ChatActivity.RESULT_OK,confirmedData);
Then, I use the following to get the results in my first activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == ChatActivity.RESULT_OK){
ArrayList<DiscoverB> confirmedB = (ArrayList<DiscoverB>)data.getSerializableExtra("confirmedB");
Long DateInMillis = data.getLongExtra( "dateInMillis", System.currentTimeMillis());
Log.d("ERROR", "error");
}
if (resultCode == ChatActivity.RESULT_CANCELED) {
}
}
}
Now, I had like to use confirmedB Once a button in my activity is clicked.
How can I pass the values from onActivityResult to my ClickListener?
ChatAdapter.OnConfirmClickListener confirmListener = new ChatAdapter.OnConfirmClickListener(){
#Override
public void onClick(Button confirmB) {
???myarraylist = onActivityResult(requestCode, resultCode, data)???
HERE I NEED MY confirmedB
}
};
Thank you
In your first activity, declare a variable
ArrayList<DiscoverB> confirmedB;
In OnActivityResult, you can initialize confirmedB
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == ChatActivity.RESULT_OK){
confirmedB = (ArrayList<DiscoverB>)data.getSerializableExtra("confirmedB");
Long DateInMillis = data.getLongExtra( "dateInMillis", System.currentTimeMillis());
Log.d("ERROR", "error");
}
if (resultCode == ChatActivity.RESULT_CANCELED) {
}
}
}
You can use it now in your clickListener,
ChatAdapter.OnConfirmClickListener confirmListener = new ChatAdapter.OnConfirmClickListener(){
#Override
public void onClick(Button confirmB) {
???myarraylist = onActivityResult(requestCode, resultCode, data)???
here you can use confirmedB
}
};

Use startActivityForResult from class to activity

Couldn't find a good answer for this. I have a class(no opened from MainActivity.
There I want to call startActivityForResult, to know when to do something on the UI.
How do I do it correctly? I passed the activity and context to the class.
On class:
private void init(){
Intent TestIntent = new Intent();
mActivity.startActivityForResult(TestIntent,MainActivity.TEST);
}
On MainActivity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
super.onActivityResult(requestCode, resultCode, Data);
if (requestCode == TEST){
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
}
Check this, You have to pass code with Intent of Required Class
private void init(){
Intent TestIntent = new Intent(this,SecondClass);
startActivityForResult(TestIntent,222);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
super.onActivityResult(requestCode, resultCode, Data);
if (requestCode == 222){
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
}
Your TestIntent is empty. It doesn't have anything in it that tells Android what Activity to start. You need to create your Intent like this:
private void init(){
Intent TestIntent = new Intent(mActivity, ActivityToStart.class);
mActivity.startActivityForResult(TestIntent,MainActivity.TEST);
}
private void init(){
Intent intent = new Intent(this,another.class);
startActivityForResult(intent,requestCode);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
super.onActivityResult(requestCode, resultCode, Data);
if (requestCode == reruestCode){
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
}
private void getResponse(){
setResult(RESULT_OK,new Intent());
finish();
//Use this block of code in the second class.
}

How to pass variable to onActivityResult() from populateViewHolder?

I have a Firebase Recycler View which contains a button like this
final String post_key = getRef(position).getKey();
viewHolder.btnUpload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("post_key",post_key);
Log.d("post_key", data.getExtras().getString("post_key"));
data.setType("image/*");
data.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(data, GALLERY_REQUEST);
}
});
This will trigger an action in OnActivityResult(). I was trying to access the variable "post_key" in OnActivityResult. Therefore, I put it to the extra, and call the getString function in OnActivityResult.
final String post_key=data.getExtras().getString("post_key");
However, the app crashed and keeps telling me that I attempt to invoke virtual method 'getString' on a null object reference
You need to check the request code and the result code as well:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
// here you can access the intent
String msg = (data != null) ? data.getStringExtra("post_key") : "";
Log.d("post_key", "Got post_key: " + msg);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
You can not send your data to another app - if it does not accept. The thing you are doing is not mentioned anywhere.
Correct Way
in onClick don't pass extra, and hold your post_key in your activity/ adapter.
adapter.setPostKey(post_key);
Then in onActivityResult() get this post_key.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
String post_key = adapter.getPostKey();
}
}
super.onActivityResult(requestCode, resultCode, data);
}

NullPointerException on Display Data(from an intent) in a TextView

I am going to build app which shows the bus connection.
My problem is that I get a nullpointer exception when I am putting a string into an intent and want to display this String in a TextView.
Here is my MainActivity:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
HaltestelleName = intent.getStringExtra("HaltestelleName");
final String Zeit = intent.getStringExtra("Zeit");
if(resultCode == Activity.RESULT_OK && requestCode == requestCode)
{
if(rgStatus.getCheckedRadioButtonId() == R.id.rdAbfahrt)
{
txtHaltestelleStart.setText("HTL Neufelden");
txtHaltestelleZiel.setText(HaltestelleName);
}
else
{
txtHaltestelleStart.setText(HaltestelleName);
txtHaltestelleZiel.setText("HTL Neufelden");
}
}
txtZeit.setText(Zeit);
super.onActivityResult(requestCode, resultCode, intent);
}
Here is my Subactivity Code:
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent haltestelle = new Intent(getApplicationContext(),
PlanActivity.class);
String item = ((TextView)view).getText().toString();
haltestelle.putExtra("HaltestelleName", item);
setResult(RESULT_OK,haltestelle);
finish();
}
hi please add the line
haltestelle.putExtra("Zeit", "yours passed value");
just below
haltestelle.putExtra("HaltestelleName", item);
Hi You didn't pass any value in your subactivity in "Zeit" name just pass some value from your sub activity to avoid null pointer exception
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
HaltestelleName = intent.getStringExtra("HaltestelleName");
final String Zeit;
if(resultCode == Activity.RESULT_OK && requestCode == requestCode)
{
if(rgStatus.getCheckedRadioButtonId() == R.id.rdAbfahrt)
{
txtHaltestelleStart.setText("HTL Neufelden");
txtHaltestelleZiel.setText(HaltestelleName);
}
else
{
txtHaltestelleStart.setText(HaltestelleName);
txtHaltestelleZiel.setText("HTL Neufelden");
}
}
if (!getIntent().hasExtra("HaltestelleName")) {
Zeit = intent.getStringExtra("Zeit");
txtZeit.setText(Zeit);
}
super.onActivityResult(requestCode, resultCode, intent);
}
The simple logic behind sending the data across two activities:
Activity A:
Intent intent = new Intent(ActivityA.this);
intent.putExtra("key", "value");
startActivity(intent);
Activity B:
Intent intent = getIntent();
String value = intent.getStringExtra("key");

About Intent requestCode, Android

Why my toast message is showing? My CEVAP_SORGULA variable equals 322, but my intents request code is 332
private final static int CEVAP_SORGULA = 322;
public void degistirActivity(final View view){
startActivityForResult(new Intent(this,veriTopla.class),332);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == Activity.RESULT_OK && requestCode == CEVAP_SORGULA ){
Toast.makeText(this, "Cevabin: " + data.getExtras().getInt("Cevap"),Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
I see clearly a type error between the CEVAP_SORGULA and the value passed via startActivityForResult. To fix it provide the variable instead of the hardcoded WRONG value.
startActivityForResult(new Intent(this,veriTopla.class),CEVAP_SORGULA);
you can try
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 332);
DEFAULT METHOD
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
SEE THIS EXAMPLE FROM ANDROID DEVELOPER SITE, MAKE YOU HELP
public class MyActivity extends Activity {
...
static final int PICK_CONTACT_REQUEST = 0;
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
// When the user center presses, let them pick a contact.
startActivityForResult(
new Intent(Intent.ACTION_PICK,
new Uri("content://contacts")),
PICK_CONTACT_REQUEST);
return true;
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}
}

Categories

Resources