我的片段概念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
}
}
你是從'HomeScreen'' Activity'添加'Screen''碎片嗎? – Bob
是的,我正在添加@鮑勃 –
這是不是很清楚你在問什麼 –