i have this fragment which makes a call to an activity using start activity for result. this activity is supposed to show a lock pattern. the problem is that the 'onActivityResult' is never called. I put some toasts to check but it never gets printed.
public class int_Create_Pattern extends Fragment {
private static final int REQ_CREATE_PATTERN = 1;
#Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
LockPatternView.MATRIX_WIDTH = 4;
Intent intent = new Intent(LockPatternActivity.ACTION_CREATE_PATTERN,
null, getActivity().getBaseContext(), LockPatternActivity.class);
startActivityForResult(intent, REQ_CREATE_PATTERN);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(resultCode, resultCode, data);
switch (requestCode) {
case REQ_CREATE_PATTERN: {
if (resultCode == LockPatternActivity.RESULT_OK) {
char[] pattern = data
.getCharArrayExtra(LockPatternActivity.EXTRA_PATTERN);
DataBaseHandler handler = new DataBaseHandler(getActivity()
.getApplicationContext());
handler.open();
String PatternToWrite = new String(pattern);
handler.createPattern(PatternToWrite);
handler.close();
Log.d("DEBUG", new String(pattern));
Toast.makeText(getActivity().getApplicationContext(),
"Pattern Recorded", Toast.LENGTH_LONG).show();
}
if (resultCode == LockPatternActivity.RESULT_CANCELED) {
Toast.makeText(getActivity().getApplicationContext(),
"Pattern Cancelled", Toast.LENGTH_LONG).show();
}
break;
}// REQ_CREATE_PATTERN
}
}
}
onActivityResult should be an Activity method, and not be on a Fragment.
Another problem is that you shouldn't call a new Activity from a Fragment. Implement an Interface for comunication, as described by the link bellow
http://developer.android.com/training/basics/fragments/communicating.html
The activity that holds your fragment should be the responsable for calling the new activity, and processing the result.
In Parent Class: do this code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}
In Child Class:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//in fragment class callback
}
Related
I have a problem with the response of my activity. It is a module to react natively. I have this react class and I am trying to get result from my activity. I've tried catching the requestCode like this:
public CameraOpenerModule(ReactApplicationContext reactContext) {
super(reactContext);
getReactApplicationContext().addActivityEventListener(new ActivityEventListener() {
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("TEST", "listener start event");
if (requestCode == 9999) {
Log.e("TEST", "request code ");
}
}
});
}
and I open the class intent:
private void openCameraIntent(){
cameraIntent = new Intent(getReactApplicationContext(), CameraHelpActivity.class);
// cameraIntent.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
cameraIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getReactApplicationContext().startActivityForResult(cameraIntent, 9999,null);
}
and another activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setResult(1111);
Log.i("ACTIVITY", "finish !!!!!");
finish();
}
Everything is ok. But I'm not catching the request code. Does anybody know why? Thanks
Add to MainActivity.java
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(mReactInstanceManager != null){
mReactInstanceManager.onActivityResult(requestCode,resultCode,data);
}
}
I have been fighting with this problem for hours now and have looked at pretty much every other post about this issue but can't find where I am going wrong. Please let me know if I am missing something simple.
Activity 1 ( hosts a CreateFragment)
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
((CreateFragment) getFragmentManager().findFragmentByTag(CreateFragment.TAG)).resultReturned(requestCode, resultCode, data);
}
CreateFragment:
private void chooseContact() {
Intent chooseContactInappIntent = new Intent( getActivity(), ContactPickerActivity.class );
getActivity().startActivityForResult( chooseContactInappIntent, PICK_CONTACT_REQUEST );
}
public void resultReturned( int requestCode, int resultCode, Intent data ) {
switch ( requestCode ) {
case PICK_CONTACT_REQUEST:
if ( resultCode == Activity.RESULT_OK ) {
//Do Cool Things
default:
break;
}
}
ContactPickerActivity:
#Override
public void onContactSelected( Object data) {
Intent returnIntent = new Intent();
returnIntent.putExtra("data",data);
setResult(RESULT_OK,returnIntent);
finish();
}
Activity One hosts a CreateFragment, when the use clicks a button in the CreateFragment the fragment calls the chooseContact() method which starts a new ActivityForResult. The ContactPickerActivity then displays the user a list of contacts. When the user selects a contact the onContactSelected method is called creating a new intent to pass back the selected data and then calls finish. I thought this would either call the onActivityResult in the base activity or the CreateFragment, but neither are called.
Any Ideas?
Thanks,
Nathan
In Fragment class work fine if you call at MainActivity onActivityResult override method and you don't need to changes inside the fragment class.
So
In Fragment that calls startActivityForResult like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && (requestCode == GALLERY_REQUEST || requestCode == CAMERA_REQUEST)) {
// its not complicated .....
}
}
In MainActivity Class are must to call onActivityResult Also.
#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);
}
}
On my fragment I am doing:
Intent intent = new Intent(getActivity(), OtherActivity.class);
startActivityForResult(intent, RETURN_CODE);
I have overriden onActivityResult on my Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
logger.debug("!!ACTIVITY!!!Returned from the activity!!!!");
super.onActivityResult(requestCode, resultCode, data);
}
The same goes for the fragment! The problem is that this method is only being called on the Activity and not in the fragment. Any clue why?
there is an issue with support v4, try using onPostResume
in onActivityResult in Activity use following code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
returningWithResult = true;
this.mData = data;
mRequestCode = requestCode;
mResultCode = resultCode;
}
and in onPostResume
#Override
protected void onPostResume() {
// TODO Auto-generated method stub
super.onPostResume();
if (returningWithResult)
<your fragment>.actionOnActivityResult(mRequestCode, mResultCode, mData);
returningWithResult = false;
}
also trying launching intent for result from activity only
public void launchIntent(Intent intent, int code) {
startActivityForResult(intent, code);
}
In Fragment use
getActivity().launchIntent(intent, PICK_FROM_CAMERA);
I dont know the reason, but it worked for me. Hope it helps.
My application have 3 tabs. In this one tab have multiple activities(means this tab have navigation to the child tabs). I used the startActivityforResult() in one child activity. But control never goes to onActivityResult() method. How to implement this. please can anybody help me.
code
public class Activity_1 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
View contentView = LayoutInflater.from(getParent()).inflate(R.layout.static_search_filters, null);
setContentView(contentView);
states_tv = (TextView)findViewById(R.id.state);
states_tv.setOnClickListener(states_etListener);
}
private OnClickListener states_etListener = new View.OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent(getParent(), RB_CategoriesMList.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivityForResult(intent,GET_SEL_STATES_LIST);
}
};
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//I do some stuff here
}
}
//Activity_2
public class RB_CategoriesMList extends ListActivity
{
public Button sbtn;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.categories_list);
sbtn =(Button)findViewById(R.id.submit_categories);
sbtn.setOnClickListener(sbtn_listener);
}
private OnClickListener sbtn_listener = new View.OnClickListener()
{
public void onClick(View v)
{
Intent state_intent = getIntent();
state_intent.putExtra("selected_states", "");
setResult(RESULT_OK,state_intent);
finish();
}
};
}
Dont call
super.onActivityResult(requestCode, resultCode, data);
When you receive the onActivityResult, check if the request code is your and then do your stuff. In the other cases you should call the super.
So:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == GET_SEL_STATES_LIST) {
// Do your stuff
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
getParent().startActivityForResult(yourNewIntent, yourResult);
I am using a simple function OnActivityResult, but it is not returning me desired results.
Please see my code and tell me where i am doing wrong.
public void OnClickFunction(View view)
{
Intent intent = new Intent(getApplicationContext(), Second.class);
startActivityForResult(intent, RESULT_OK);
/// My actions. . .
}
Then in the Second class, i have set Result like this:
Button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
ValueOne = EditText.getText().toString().trim();
if (ValueOne.equals(String.valueOf(Answer)))
{
Toast.makeText(getApplicationContext(), "Correct Answer", 0).show();
Second.this.setResult(RESULT_OK, null);
Second.this.finish();
}
else
{
Toast.makeText(getApplicationContext(), "Wrong Answer", 0).show();
}
}
});
Now coming back to the first.class, from where the Intent was called:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// if (requestCode == RESULT_OK)
// {
if (resultCode == RESULT_OK)
{
///// MyActions. . .
}
// }
}
The debugger is not debugging this function, so the desired results are not coming.
Where i am doing wrong??
You have to destroy the second activity. Try pressing back button. I am able to see all the log messages in onActivityResult
First Activity
public class FirstActivity extends Activity {
/** Called when the activity is first created. */
int result = 100;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this,SecondActivity.class);
startActivityForResult(i, result);
}
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
Log.i("H", "RequestCode:" + requestCode);
Log.i("H", "ResultCode:" + resultCode );
}
}
SecondActivity
public class SecondActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setResult(RESULT_OK);
Log.i("S","Exiting Second Activity");
}
}
in Source Class:
int activity=1;
Intent i=new Intent(Sourceclass.this,destination.class);
startActivityForResult(i,activity);
In Destination class:
Intent i=new Intent();
setResult(RESULT_OK,i);
finish();
In OnActivityResult of Source Class:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if(requestCode==1)
{
Log.e("check","check");
}
}
}
in my case: when use activityForResult, I add this flag :
addFlags(FLAG_ACTIVITY_NEW_TASK)
when delete this flag ,fixed