0

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

        }

    }

   }
Remees M Syde
  • 2,564
  • 1
  • 19
  • 42
Clinton Dsouza
  • 330
  • 4
  • 20

2 Answers2

0

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.

Renan Ferreira
  • 2,122
  • 3
  • 21
  • 30
  • so i should just call another activity from the fragment and then process all my info in that activity? – Clinton Dsouza Nov 17 '14 at 10:26
  • No. In your fragment, you should tell your activity that it needs to start a new activity for result. You can do it using an interface, as decribed by the link. The "onActivityResult" should be in this first activity. If you would like to pass the result to the fragment, you can implement the code described in the same link http://developer.android.com/training/basics/fragments/communicating.html#Deliver – Renan Ferreira Nov 17 '14 at 10:31
0
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 
} 
koutuk
  • 832
  • 1
  • 8
  • 17