How to call onActivityResult from non activity class - android

I'd like to get data when startActivityForResult from non Activity, please for me some sugession.
For example:
Fragment A--> non Activity class, startActivityForResult here, and how to get data on FragmentA
FragmentA
DownloadFile files = DownLoadFile.loadFiles(getActivity)
class DownloadFile
{
public static void loadFiles()
{
Intent = new Intent(...)
startActivityForResult
}
}

u can do a callback on FragmentA and in DownloadFile to invoke it!

onActivityResult() is a method in the class Activity. Therefore if you're not extending Activity, you can't override it
You need an Activity on order to receive the result of the action. If it is just for reuse and organisation then just call your other class from your Activity?
public class DownloadFile {
public static void loadFiles(int requestCode, int resultCode, Intent data){
...
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
DownloadFile.loadFiles(requestCode,resultCode,data);
...
}

Related

startActivityforResult from adapter

I have an adapter inside a fragment, in this adapter I am running an activityForResult to an activity.
Calling the adapter from the Fragment
mAdapClinicSchedule = new ClinicScheduleAdapter(getContext(),mScheduleList);
mLvClinicsSchedules.setAdapter(mAdapClinicSchedule);
Adapter's constructor:
public ClinicScheduleAdapter(Context context, ArrayList<String> mScheduleList) {
this.context = context;
this.mScheduleList = mScheduleList;
}
The call to the startActivityForResult from the adapter
case R.id.tvDaysClinicSchedule:
Intent intent = new Intent(context, ScheduleDaysActivity.class);
((Activity)context).startActivityForResult(intent, 2);
break;
The setResult from the activity
case R.id.tvSelectDays:
checkDaysSelected();
Intent returnIntent = new Intent();
returnIntent.putExtra("monday", mondaySelected);
returnIntent.putExtra("tuesday", tuesdaySelected);
returnIntent.putExtra("wednesday", wednesdaySelected);
returnIntent.putExtra("thursday", thursdaySelected);
returnIntent.putExtra("friday", fridaySelected);
returnIntent.putExtra("saturday", saturdaySelected);
returnIntent.putExtra("sunday", sundaySelected);
setResult(Activity.RESULT_OK, returnIntent);
finish();
break;
Creating an onActivityResult interface
public interface OnActivityResult {
void onActivityResult(int requestCode, int resultCode, Intent data);
}
That implements the adapter:
public class ClinicScheduleAdapter
extends BaseAdapter implements View.OnClickListener, OnActivityResult { ... }
In the fragment it overrite the onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
mAdapClinicSchedule.onActivityResult(requestCode, resultCode, data);
//super.onActivityResult(requestCode, resultCode, data);
}
But in the adapter, it does not show the toast, I do not know what I can be doing wrong
public void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode == 2) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(context, "it worked!", Toast.LENGTH_LONG).show();
}
}
}
From the android documentation:
Of course, the activity that responds must be designed to return a result. When it does, it sends the result as another Intent object. Your activity receives it in the onActivityResult() callback.
Although, if I remember correctly, onActivityResult() will get called on whatever started the activity for result. So if you use the activity to start your new intent, then the activity will receive the result call. This means if you want the fragment to handle it, you'll need to call startActivityForResult(...) on the fragment rather than the activity
fragment source

resultCode 0 and data zero in onActivityResult

I have activity with one fragment, and then that fragment call some activity which that activity will give some value into first activity. i use onActivityResult but i don't know why resultCode always 0 and data always zero,.
on Activity One i have
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: "+ requestCode +" "+resultCode+" "+data);
}
Activity one have some fragmentX which call activity two
private final int REQUEST_CODE = 10;
private void start (){
Intent intent = new Intent(getContext(),Main2Activity.class);
intent.putExtra("xxx","test1");
getActivity().startActivityForResult(intent,REQUEST_CODE);
}
then in Activity Two, when i touch onBackpress, will pass some value.
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = getIntent().putExtra("yyy","test2");
setResult(RESULT_OK,intent);
}
}
but, i don't know why, onActivityResult i can't get the data.
my final purpose is, i want to setArgument that data to fragmentX.
1.You can call onActivityResult in the fragment
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: "+ requestCode +" "+resultCode+" "+data);
}
2.Use super.onBackPressed(); after setResult in your method
#Override
public void onBackPressed() {
Intent intent = getIntent().putExtra("yyy","test2");
setResult(RESULT_OK,intent);
super.onBackPressed();
}
In first activity write the below code:
1st method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
for (Fragment fragment : getSupportFragmentManager().getFragments()) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
In Fragment
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// In fragment write your logic here
}
2nd method: (I didn't try but as per doc may be this is one of the reason try once) if it will not work go 1st method its 100 percent solution
Here when your calling second activity using getContext()
getContext() - Returns the context view only current running activity.
getActivity()- Return the Activity this fragment is currently associated with.
Try instead of getContext() use getActivity() when you calling intent to next activity
Ex:
private final int REQUEST_CODE = 10;
Intent intent = new Intent(getActivity(),Main2Activity.class);
intent.putExtra("xxx","test1");
getActivity().startActivityForResult(intent,REQUEST_CODE);
For start new activity
private final int REQUEST_CODE = 10;
private void start (){
Intent intent = new Intent(getContext(),Main2Activity.class);
intent.putExtra("xxx","test1");
getActivity().startActivityForResult(intent,REQUEST_CODE);
}
From Second activity
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = getIntent().putExtra("yyy","test2");
setResult(10,intent);
}
}
you have to set the integer code using which you have started second activity. you have started second activity with the code "10".so use the same value when you setResult(10,intent);
onActivityResult you need to check Request code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==10){
if(data!=null)
{
//you have to manage your own list of fragments for activity because getSupportFragmentManager().getFragments() is deprecated now.
//Send data to fragments
if (arrayFragments != null) {
for (Fragment fragment : arrayFragments) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
}
}
}
Remember you have to manage your own list of fragments using ArrayList or HashMap.
Hope this will help you if you need any other help inform me.
if you wants to send return data in onActivityResult then you should use super.onBackPressed() after data set in setResult() method
example:
#Override
public void onBackPressed() {
val intent = Intent()
intent.putExtra("IsReturnData", true)
setResult(RESULT_OK,intent);
super.onBackPressed(); //write this line at the end of method
}

How to refresh previous fragment after closing an activity?

I have a fragment that throws an activity in which user can insert some details.
When the user terminate to fill data the activity close. Then the previous fragment is showed and I want it to be updated (with the data provided by user in the activity).
If it was an activity, instead of fragment, i could use this:
#Override
public void onResume(){
super.onResume();
recreate();
}
How to do the same with fragment?
You would override onResume in the Activity which this Fragment belong, and recreate (restarted) this Fragment in this Activity. For example
public class ActivityA extends Activity {
#Override
public void onResume() {
// here you can get the Fragment instance , so you can recreated this fragment here or invoke this fragment's function to set data or refresh data
}
}
You can throw an Activity in calling startActivityForResult (Intent intent, int requestCode) method in your fragment.
also you need to provide onActivityResult (int requestCode, int resultCode, Intent data) in your fragment,the data is in the Intent parameter.
public class YourFragment extends Fragment {
public void throwActivity() {
// start activity
Intent intent = new Intent();
intent.setClass(getActivity(), YourActivity.class);
int requestCode = 1;
startActivityForResult(intent,requestCode);
}
// callback
#Override
public void onActivityResult (int requestCode, int resultCode, Intent data) {
recreate();
}
public void recreate() {
// your refresh code
}
}
public class YourActivity extends Activity {
public void yourMethod() {
int resultCode = 2;
setResult(resultCode); // goback to your fragment with your data
}
}

onActivityResult not called from main class

I have a problem with my application.I have an activity called ExperimentView which has an inner class called FrameRoll which implements Runnable.
public class ExperimentView extends Activity implements OnClickListener,{
public void onCreate(Bundle savedInstanceState) {
//Other stuff
//Call the gpshandler activity
gpsCall();
}
public void gpsCall() {
if (gpsEnabled.equalsIgnoreCase("true")) {
Intent gpsHandler = new Intent(ExperimentView.this,
GpsHandler.class);
startActivityForResult(gpsHandler,
GPSHANDLER_ACTIVITY_RETURN);
finish();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//retrieve the data
}
public class FrameRoll implements Runnable {
//do other stuff
}
}
I'm calling a subactivity from the onCreate method in order to retrieve some gps data.The gps data are retrieved successfully.
public class GpsHandler extends Activity {
//obtain location
enter code here
Intent resultIntent = new Intent();
resultIntent.putExtra("location",
optimalLocation.toString());
setResult(Activity.RESULT_OK, resultIntent);
}
When i try to call the subactivity from the inner class (frameroll) the sub activity runs normally but the method onActivityResult in ExperimentVIew is never called,so i don't get any data back.
public class ExperimentView extends Activity implements OnClickListener,{
public void onCreate(Bundle savedInstanceState) {
//Other stuff
//Call the gpshandler activity
gpsCall();
}
public void gpsCall() {
if (gpsEnabled.equalsIgnoreCase("true")) {
Intent gpsHandler = new Intent(ExperimentView.this,
GpsHandler.class);
startActivityForResult(gpsHandler,
GPSHANDLER_ACTIVITY_RETURN);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//retrieve the data
}
public class FrameRoll implements Runnable {
gpsCall();
}
}
I've tried different stuff which included implementing onActivityResult in the nested class,trying to do something like ExperimentView.this.startActivityForResults(),I printed the callingActivity in the gpshandler and it was the ExperimentView.
Every method i tried just failed.The onActivityResult is called only when the subactivity was called from onCreate.Does anynone know what should i do?
Thanx :)
onActivityResult is called once your called-activity is finished. Therefore, you need to call finish(); after setting result. For example:
setResult(Activity.RESULT_OK, resultIntent);
finish();
*Make sure you are GpsHandler class complies with the correct implementation of Activity as it doesn't seem to override onCreate and/or other methods.

Clearing task and start a new activity

I don't know why this is so difficult to figure out. I have my main activity that, when launched, checks if this is the first time it's been opened. If it is, then it closes the main activity and opens the setup/introduction activity with FLAG_ACTIVITY_NEW_TASK. The setup process consists of three activities (A, B, and C). At the end of activity C, how do I get it to clear and the setup task that contains A, B, and C and start the main activity again. I've tried adding FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP to main activity Intent but when I press BACK, it returns to activity C of the setup process. How do I get it to clear the task of activities A, B, and C when C finishes and starts the main? Thanks!
I'm building in Android 1.6 (API 4), so some of the Activity flags may be limited.
FLAG_ACTIVITY_CLEAR_TOP will clear activities it it is of the same activity instance. Here in your case all your activities are of different instances so FLAG_ACTIVITY_CLEAR_TOP won't work. To clear your task, create an Activity instance in each of your activity and assign that instance 'this' on your onCreate method of every activity. whenever you want to clear your task jus call that instance.finish(). and start the activity that you want.
Actually this can be achieved with startActivityForResult
public class A extends Activity {
public onButtonClick() {
startActivityForResult(new Intent(this, B.class), 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
finish();
}
}
}
public class B extends Activity {
public onButtonClick() {
startActivityForResult(new Intent(this, C.class), 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
setResult(RESULT_OK);
finish();
}
}
}
public class C extends Activity {
public onButtonClick() {
setResult(RESULT_OK);
finish();
}
}
I think this is the right way, you don't leak anything this way.
PS: I know this is old post, but maybe someone will find this useful.
I answered a similar question here
As Mudassir says in their comment just finish() your activities immediately after starting a new one.
class A extends Activity {
public static Activity instanceOfA = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instanceOfA = this;
}
}
class b extends Activity {
public static Activity instanceOfB = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instanceOfB = this;
}
}
class c extends Activity {
public static Activity instanceOfC = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
instanceOfC = this;
}
}
Now suppose you want to clear all the task from your current activity, then call
instanceOfA.finish();
instanceOfB.finish();
instanceOfC.finish();

Categories

Resources