2017-08-20 97 views
0

我的片段概念here.Here工作我面對這裏的問題是,我需要只給運行時間許可,不得以HomeScreen.java類,所以我有後致電呼叫方法Screen.java一直在嘗試這個,但我不能得到正確的解決方案,所以你可以爲我提供適當的解決方案。如何從Android中的Activity調用Fragment中的方法?

public class HomeScreen extends DrawerActivity { 

public static final int MULTIPLE_PERMISSIONS = 10; // code you want. 
String[] permissions = new String[] { 
     Manifest.permission.ACCESS_COARSE_LOCATION, 
     Manifest.permission.ACCESS_FINE_LOCATION }; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    Log.e("passing in home", "passing in home"); 
    checkPermission(Home.this); 
    ctx = this; 

} 


private boolean checkPermission(Activity act) { 

    int result; 
    List<String> listPermissionsNeeded = new ArrayList<>(); 
    for (String p : permissions) { 

     result = ContextCompat.checkSelfPermission(act, p); 
     if (result != PackageManager.PERMISSION_GRANTED) { 
      listPermissionsNeeded.add(p); 
     } else { 

     } 
    } 
    if (!listPermissionsNeeded.isEmpty()) { 

     ActivityCompat.requestPermissions(act, listPermissionsNeeded 
       .toArray(new String[listPermissionsNeeded.size()]), 
       MULTIPLE_PERMISSIONS); 

     return false; 
    } 
    return true; 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, String[] arg1, 
     int[] grantResults) { // TODO Auto-generated method stub 
    super.onRequestPermissionsResult(requestCode, arg1, grantResults); 

    switch (requestCode) { 
    case MULTIPLE_PERMISSIONS: { 
     if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

     } else { 

     } 
     return; 
    } 

    } 
} 

@Override 
public void onBackPressed() { 
    // TODO Auto-generated method stub 
    currentPosition = -1; 
    super.onBackPressed(); 
} 

} 

// 2級

public class Screen extends Fragment{ 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
call(); 
} 
public void call(){ 
//my code 
} 
} 
+0

你是從'HomeScreen'' Activity'添加'Screen''碎片嗎? – Bob

+0

是的,我正在添加@鮑勃 –

+0

這是不是很清楚你在問什麼 –

回答

1

可以使用layout id獲得從FragmentManagerFragment實例。

Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container); 
// fragment_container is the id of the container layout where you had added your fragment 

它投射到您的Fragment,並呼籲:

Screen screen = (Screen) fragment; 
screen.call(); 
+0

我不太清楚你在提及哪個ID。謝謝回覆bob @覆蓋 public view onCreateView(LayoutInflater inflater,ViewGroup容器, Bundle savedInstanceState) {vt。} {}}}}}} return v; } –

+1

當你在活動中添加一個片段時,你應該使用一個ID:'fragmentTransaction.add(R.id.fragment_container,fragment);'傳遞該ID。 – Bob

0

使用標籤添加片段

String TAG="MyFragment"; 
FragmentManager fragmentManager = getSupportFragmentManager(); 
fragmentManager.beginTransaction() 
     .replace(R.id.fragment_container, mFragment, TAG) 
     .commit(); 

您可以從您的活動調用一個方法,像這樣

MFragment fragment = (MFragment) getFragmentManager().findFragmentByTag(TAG); 
if (fragment) { 
fragment.testMethod(); 
} 
相關問題