2014-07-13 76 views
0

這是我placeholderfragment取代片段onActivityResult

public static class PlaceholderFragment extends Fragment { 

     private static final String ARG_SECTION_NUMBER = "section_number"; 

     public static int SectionNumber; 
     public static LayoutInflater Inflater; 
     public static ViewGroup Container; 
     public static Bundle SavedInstanceState; 

     public static PlaceholderFragment newInstance(int sectionNumber) { 
      SectionNumber = sectionNumber; 
      PlaceholderFragment fragment = new PlaceholderFragment(); 
      Bundle args = new Bundle(); 
      args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
      fragment.setArguments(args); 
      return fragment; 
     } 

     public PlaceholderFragment() 
     { } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      Inflater = inflater; 
      Container = container; 
      SavedInstanceState = savedInstanceState; 
      View rootView = null; 

      switch (SectionNumber) 
      { 
       case 1: 
        rootView = inflater.inflate(R.layout.fragment_main, container, false); 
        break; 
       case 2: 
        Intent intent = new Intent("com.google.zxing.client.android.SCAN"); 
        intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE"); 
        startActivityForResult(intent, 0); 
        break; 
       case 3: 
        rootView = inflater.inflate(R.layout.fragment_main, container, false); 
        marketActivity = new MarketActivity(context); 
        marketActivity.loadMarkets(rootView); 
        break; 
       case 4: 
        rootView = inflater.inflate(R.layout.fragment_main, container, false); 
        break; 
      } 
      return rootView; 
     } 

     @Override 
     public void onAttach(Activity activity) { 
      super.onAttach(activity); 
      ((MainActivity) activity).onSectionAttached(
        getArguments().getInt(ARG_SECTION_NUMBER)); 
     } 

     public void onActivityResult(int requestCode, int resultCode, Intent intent) 
     { 
      Log.e("onActivityResult", "1"); 
      if(requestCode == 0)  { 

       if(resultCode == RESULT_OK)   { 
        Log.e("onActivityResult", "3"); 


        String contents = intent.getStringExtra("SCAN_RESULT"); 
        String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); 

        movementActivity = new MovementActivity(context); 
        movementActivity.setUp(contents); 

        Inflater.inflate(R.layout.fragment_movement, Container, false); 
        Log.e("onActivityResult", "2"); 
        Log.i("xZing", "contents: "+contents+" format: " 
          +format); 

        // Handle successful scan 
       } 
       else if(resultCode == RESULT_CANCELED) 
       {    // Handle cancel 
        Log.i("xZing", "Cancelled"); 
       } 
      } 
     } 
    } 

面對我的是onActivityResult當resultCodeRESULT_OK我想替換Inflater.inflate(R.layout.fragment_movement, Container, false);當前片段,我希望我做我的問題明確到u的問題,即時通訊真的很抱歉mybad英語

回答

0

也許有這樣一個簡單的方法,但你可以做的是重寫實際片段中的onActivityResult方法,你正在開始結果的活動。

在您的活動從代管片段的接口處理回調

public interface ActivityResultCallbacks { 

     void onResultOK(int request, int result); 
    } 

在您的片段投實現和存儲參考回調

private ActivityResultCallbacks mCallbacks; 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     try { 
      mCallbacks = (ActivityResultCallbacks) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException("Activity must implement ActivityResultCallbacks."); 
     } 
    } 

通知回調在片段的onActivityResult

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent intent){ 
    mCallbacks.onResultOK(requestCode, resultCode); 
} 

在您的父母活動中,回調開關片段

@Override 
public void onResultOK(int request, int result){ 
    //check for result and requestcode 
    Fragment fragment = new YourFragment(); 
    FragmentManager fragmentManager = getFragmentManager(); 
    fragmentManager.beginTransaction().replace(R.id.YourContainer, fragment).commit(); 
} 
+0

方法不覆蓋或在親活性onResultOK實現從一個超類的方法 – user3806961

+0

代替onResultOK –

相關問題