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.
}
Related
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
}
};
#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 );
I want to receive data in previous Activity from next activity like (1 <--- 2 ). I tried but data is not received from second to first activity .
This is First Cativity
Intent i = new Intent(CustomActionActivity.this, Edit_Post.class);
i.putExtra("ActivityId", getItemActivity);
i.putExtra("Vessel", strVesselName);
i.putExtra("HashTag", strHashTag);
i.putExtra("RemarkTitle", strRemark);
i.putExtra("ShortRGN", strShortTypeRGN);
i.putExtra("VessId", strvesselid);
startActivity(i);
This is second Activity
Intent intent = getIntent();
strActivityId = intent.getStringExtra("ActivityId");
strVesselName = intent.getStringExtra("Vessel");
strHashTag = intent.getStringExtra("HashTag");
strRemark = intent.getStringExtra("RemarkTitle");
strShortRGN = intent.getStringExtra("ShortRGN");
strVessId = intent.getStringExtra("VessId");
img_AddPostAudio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(Edit_Post.this, EditRecord.class);
i.putExtra("EditVesselId", strVessId);
i.putExtra("EditActivityId" , strActivityId);
startActivity(i);
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE)
{
String audioString=data.getStringExtra("AUDIO_STRING");
Log.e(" audioString "," = "+audioString);
}
}
This is Third Activity
Intent intent = getIntent();
vesselId = intent.getStringExtra("EditVesselId");
strActivityId = intent.getStringExtra("EditActivityId");
Intent intent=new Intent(EditRecord.this, Edit_Post.class);
intent.putExtra("AUDIO_STRING",newAudioFile);
setResult(REQUEST_CODE, intent);
finish();
do this when you are calling the second activity
Intent i = new Intent(CustomActionActivity.this, Edit_Post.class);
i.putExtra("HashTag", strHashTag);
startActivityForResult(i, REQUEST_CODE);
Now you need to set the result what you want on CustomActionActivity
e.g.
Intent intent=new Intent();
intent.putExtra("MESSAGE",message);
setResult(REQUEST_CODE,intent);
finish();
Now you will get this data to the your first activity
e.g.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_CODE)
{
String message=data.getStringExtra("MESSAGE");
textView1.setText(message);
}
}
let me know in case of any issue
Use SharedPreferences
Store the data in a SharedPreference and access it in from the SharedPreference in the other activity.
Add the data to the SharedPreference in the onStop for the 2nd Activity and access it in the onCreate of the other Activity
#Override
public void onStop(){//Where you wish to insert data
SharedPreferences data=getSharedPreferences(PREFS_FILE,0);
SharedPreferences.Editor editor= count.edit();
editor.put("data","DATA");
editor.apply();
super.onStop();
}
in onCreate() of the other Activity:
SharedPreferences data = getApplicationContext().getSharedPreferences(PREFS_FILE, 0);
String dataString=data.get("Data",0);
Hope this helps. Cheers.
I am doing this way its works for me:
Activity 2:
public void onBackPressed() {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra("MESSAGE", strtext + "");
setResult(2, intent);
if (isclose) {
finish();
} else {
super.onBackPressed();
}
}
}
Activity1:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String sSuName = data.getStringExtra("MESSAGE");
//txtfavouratecount.setText(sSuName);
}
in Your onclick listener
Intent itemintent = new Intent(context,your target Activity.class);
Bundle b = new Bundle();
b.putStringArray("iarray", Qtag);
b.putInt("mflag", 0);
itemintent.putExtra("android.intent.extra.INTENT", b);
startActivityForResult(itemintent,2);
I would do something like this:
In Activity A:
private static final int REQUEST_CODE = 9001;
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, REQUEST_CODE);
In Activity B:
Intent data = new Intent();
data.putExtra("key", parameter);
setResult(CommonStatusCodes.SUCCESS, data);
finish();
And finally, in the Activity A, receive result:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == CommonStatusCodes.SUCCESS) {
if (data != null) {
String result = data.getStringExtra("key");
}
} else {
//Error to receive data
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
}
Good luck!
I try onActivityResult in fragment currently I am using v4 fragment.
my activity code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.v(tag, "0000");
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
}
My fragment code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.v(getTag(), "in 0");
if (resultCode == Activity.RESULT_OK) {
Log.v(getTag(), "in 1");
if (requestCode == MY_INTENT_CLICK) {
Log.v(getTag(), "in 2");
if (null == data)
return;
Log.v(getTag(), "in 3");
Uri selectedImageUri = data.getData(); } } }
I call start Intent in
Log.v(getTag(), "in 00");
Intent intent = new Intent();
Log.v(getTag(), "in 01");
intent.setType("video/*");
Log.v(getTag(), "in 02");
intent.setAction(Intent.ACTION_GET_CONTENT);
Log.v(getTag(), "in 03");
fragment.startActivityForResult(Intent
.createChooser(intent,
"Select a file"),
MY_INTENT_CLICK);
I also try call intent in
getActivity().startActivityForResult(Intent
.createChooser(intent,
"Select a file"),
MY_INTENT_CLICK);
startActivityForResult(Intent
.createChooser(intent,
"Select a file"),
MY_INTENT_CLICK);
But noting is work. I also refer.
Fragment Compatability onActivityResult() Not Working
onActivityResult is not being called in Fragment
onActivityResult() not called in new nested fragment API
Refer below code
public class RegisterDeRegisterResponse {
private Vector<IResultResponse> _iCallBack = new Vector<IResultResponse>();
public static RegisterDeRegisterResponse ref = null;
public static RegisterDeRegisterResponse getInstance() {
if (ref == null) {
ref = new RegisterDeRegisterResponse();
}
return ref;
}
/**
* Register to recieve server response.
*/
public void registerForServerResponse(IResultResponse callback) {
_iCallBack.addElement(callback);
}
/**
* Notify all registered users about server response with corresponding
* process id.
*/
public void notifyRegisteredUser(int requestCode, int resultCode,
Intent data) {
Enumeration<IResultResponse> enumeration = _iCallBack.elements();
while (enumeration.hasMoreElements()) {
IResultResponse callback = enumeration.nextElement();
callback.onActivityResult(requestCode, resultCode, data);
}
}
/**
* Deregister to recieve server response
*/
public void deRegisterForServerResponse(IResultResponse callback) {
_iCallBack.removeElement(callback);
}
}
And also make Interface which have onActivityResult method declaration
public interface IResultResponse {
public void onActivityResult(int requestCode, int resultCode, Intent data);
}
Now in class in which you are calling startActivityForResult put this line before startActivityForResult and implement IresultResonse also
RegisterDeRegisterResponse register1 = RegisterDeRegisterResponse
.getInstance();
register1.registerForServerResponse(this);
and also deregister in onActivityResult
RegisterDeRegisterResponse
.getInstance().deRegisterForServerResponse((IResultResponse)this);
When I my app is launched I am showing a splash screen. That page is been shown for 10 sec, running on a thread.
When it switches over to new activity on a result I want o hit a URL in server and I will be getting a return value which I can use for my further implements.
Here is my code:
private final int SPLASH_DISPLAY_LENGHT = 1000;
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
Log.e("Handler ","run");
Intent myIntent = new Intent(getApplicationContext(), CaptureActivity.class);
startActivityForResult(myIntent, imgDL);
finish();
}
}, SPLASH_DISPLAY_LENGHT);
public void onActivityResult(int requestCode, int resultCode, final Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == imgDL)
{
Log.e("onActivity Result","");
urlHitMethod("http://XXXXXXXXXXXXXXXXXX.com/banner_scan");
}
}
But here the onActivityResult is not called. How to fix this?
Also, please note than if you base activity (the one calling startActivityForResult) can't use the flag noHitory in the manifest.
If you do so, onActivityResult will never be called.
try this
Intent myIntent = new Intent(activity.this, CaptureActivity.class);
and
#Override
public void onActivityResult(int requestCode, int resultCode, final Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == imgDL)
{
Log.e("onActivity Result","");
urlHitMethod("http://XXXXXXXXXXXXXXXXXX.com/banner_scan");
}
if(resultCode==RESULT_OK)
{
Log.e("onActivity Result","come in onactivity result ok");
}
else
{
Log.e("onActivity Result","come in onactivity result with error");
}
}
If you are using onActivityResult, then you should not finish the activity when starting with intent otherwise it will crash the app.
Thanks.
In the CaptureActivity.class you have to set the result and then check in the onActivityResult in the First Activity the result code
In the CaptureActivity.class it should be like the following
Intent in = new Intent();
setResult(1,in);//Here I am Setting the Requestcode 1, you can put according to your requirement
finish();