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.
Related
I have a plane java class which accepts an Activity in constructor. Now with that activity I am starting another activity for some permissions. Now I want to wait for this activity to complete before proceeding next.
public final class Test{
#NonNull
private final Activity activity;
public Test(final Activity activity) {
this.activity = activity;
}
public void initialize() {
try {
if(isEnabled) {
this.activity.startActivity(new Intent(this.activity, CheckPermissionsActivity.class));
}
FileUtils.readFile(Constants.XX+ Constants.YY);
}
I want to make this readFile method run after the CheckPermissionsActivity is complete. How to make the main thread wait?
if you have a separate class for permission checking then do something like this.
NOTE: I'm only explaining the basic concept. do the changes according to your requirements.
Intent intent = new Intent(this, CheckPermissionsActivity.class);
startActivityForResult(intent, 101);//101=requestCode
// write this according to your requirements in CheckPermissionsActivity
Intent i = new Intent();
i.putExtra("result", mString);
if (mString.length() > 2) {
setResult(Activity.RESULT_OK, i);
} else {
setResult(Activity.RESULT_CANCELED, i);
}
finish();
// in your main activity use this callback and use in your Test class
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101 && data != null) {
// write your code here
}
}
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);
...
}
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
}
i start activity for result to open GPS
then recieve in OnACtivityResult .. it is work fine in some mobiles , but with sony not work , i don't know why
i use nested fragment
start activity
Intent callGPSSettingIntent = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(
callGPSSettingIntent, openGPSRequest);
then receive in fragment
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == openGPSRequest) {
try {
activity.hideProgress();
} catch (Exception e) {
}
activity.replaceCurrentFragment(
FramentNavigationMapInside.getInstance(branche), true, true);
}
}
in main activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
and in every fragment override onActivityResult
and i test it in lenove and samsung and work fine ,, but with sony xperia not work, only onActivityResult which in main activity call
solve my issue by
One of the common problem we always meet in the world of Fragment is: although we could call startActivityForResult directly from Nested Fragment but it appears that onActivityResult would never been called which brought a lot of trouble to handle Activity Result from Nested Fragment.
the solution
So we will call getActivity().startActivityForResult(...) from Fragment instead of just startActivityResult(...) from now on. Like this:
// In Fragment
Intent intent = new Intent(getActivity(), SecondActivity.class);
getActivity().startActivityForResult(intent, 12345);
As a result, all of the result received will be handled at the single place: onActivityResult of the Activity that Fragment is placed on.
Question is how to send the Activity Result to Fragment?
Due to the fact that we couldn't directly communicate with all of the nested fragment in the normal way, or at least in the easy way. And another fact is, every Fragment knows that which requestCode it has to handled since it is also the one that call startActivityForResult. So we choose the way to "broadcast to every single Fragment that is active at time. And let those Fragments check requestCode and do what they want."
Talk about broadcasting, LocalBroadcastManager could do the job but the mechanic is the way too old. I choose another alternative, an EventBus, which has a lot of choices out there. The one that I chose was Otto from square. It is really good at performance and robustness.
dependencies {
compile 'com.squareup:otto:1.3.6'
}
In the Otto way, let's create a Bus Event as a package carry those Activity Result values.
import android.content.Intent;
/**
* Created by nuuneoi on 3/12/2015.
*/
public class ActivityResultEvent {
private int requestCode;
private int resultCode;
private Intent data;
public ActivityResultEvent(int requestCode, int resultCode, Intent data) {
this.requestCode = requestCode;
this.resultCode = resultCode;
this.data = data;
}
public int getRequestCode() {
return requestCode;
}
public void setRequestCode(int requestCode) {
this.requestCode = requestCode;
}
public int getResultCode() {
return resultCode;
}
public void setResultCode(int resultCode) {
this.resultCode = resultCode;
}
public Intent getData() {
return data;
}
public void setData(Intent data) {
this.data = data;
}
}
import android.os.Handler;
import android.os.Looper;
import com.squareup.otto.Bus;
/**
* Created by nuuneoi on 3/12/2015.
*/
public class ActivityResultBus extends Bus {
private static ActivityResultBus instance;
public static ActivityResultBus getInstance() {
if (instance == null)
instance = new ActivityResultBus();
return instance;
}
private Handler mHandler = new Handler(Looper.getMainLooper());
public void postQueue(final Object obj) {
mHandler.post(new Runnable() {
#Override
public void run() {
ActivityResultBus.getInstance().post(obj);
}
});
}
}
And in activity
public class MainActivity extends ActionBarActivity {
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ActivityResultBus.getInstance().postQueue(
new ActivityResultEvent(requestCode, resultCode, data));
}
...
}
and in the end handle back data onActivityResult in fragment
public class BodyFragment extends Fragment {
...
#Override
public void onStart() {
super.onStart();
ActivityResultBus.getInstance().register(mActivityResultSubscriber);
}
#Override
public void onStop() {
super.onStop();
ActivityResultBus.getInstance().unregister(mActivityResultSubscriber);
}
private Object mActivityResultSubscriber = new Object() {
#Subscribe
public void onActivityResultReceived(ActivityResultEvent event) {
int requestCode = event.getRequestCode();
int resultCode = event.getResultCode();
Intent data = event.getData();
onActivityResult(requestCode, resultCode, data);
}
};
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Don't forget to check requestCode before continuing your job
if (requestCode == 12345) {
// Do your job
tvResult.setText("Result Code = " + resultCode);
}
}
...
}
hope this code help any one face this problem
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
}
}