在我的设备上运行应用程序并且它在模拟器上运行不正常。 页面存储为片段,并使用底部导航栏切换。
public class HomeController extends FragmentActivity implements Serializable {
private BottomNavigationViewEx navBar;
private Fragment selectedFragment;
private HomeFragment homeActivity;
private OverviewFragment overviewActivity;
private UploadFragment uploadActivity;
private ProfileFragment profileActivity;
private SettingsFragment settingsActivity;
// Called when the page is created.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_activity);
// Setup the onclick listener for the bottom
navBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
boolean upload = false;
// Check if the same tab is not clicked twice.
if(item.getItemId() != lastNumber){
switch (item.getItemId()){
case R.id.nav_home:
selectedFragment = homeActivity;
break;
case R.id.nav_overrview:
selectedFragment = overviewActivity;
break;
case R.id.nav_upload:
selectedFragment = uploadActivity;
upload = true;
break;
case R.id.nav_profile:
selectedFragment = profileActivity;
break;
case R.id.nav_settings:
selectedFragment = settingsActivity;
break;
}
// Set the fragment holder as the selected fragment.
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
// Work out what animation to use for the fragment transition.
if(upload){
transaction.setCustomAnimations(R.anim.push_up_in, R.anim.push_up_out);
}else{
if((item.getItemId() < lastNumber) && !firstTime){
transaction.setCustomAnimations(R.anim.push_right_enter, R.anim.push_right_exit);
}else{
transaction.setCustomAnimations(R.anim.push_left_enter, R.anim.push_left_exit);
}
firstTime = false;
}
lastNumber = item.getItemId();
transaction.replace(R.id.fragmentHolder, selectedFragment);
transaction.commit();
return true;
}
return false;
}
});
//Manually displaying the first fragment - one time only
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentHolder, homeActivity);
transaction.commit();
}
然后调用文件选择器意图的片段。
public class UploadFragment extends Fragment {
private void selectFile(){
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("*/*");
startActivityForResult(intent, READ_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
// Check if the data selected is valid.
if(data != null){
copyFileForUpload(data);
}
}
}
}
在模拟器上运行时,它可以正常工作并返回正确的片段并调用onActivityResult。当我把它放在我的设备上时,只调用来自家庭控制器的onActivityResult并将片段更改回HomeFragment(默认)。
我试过了:
正如我所看到的其他一些帖子所示。
编辑:我还确保我在模拟器和设备上运行相同版本的android。问题仍然存在。
edit2:刚试过另一台设备pixelXL,它工作正常,它只是在我的三星s8上工作。可能是api等级差异s8 =等级24和像素= 27?
任何帮助都会很棒! 谢谢!
答案 0 :(得分:0)
请在Activity
中添加onActivityResult()方法@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(upload)
selectedFragment.onActivityResult(requestCode, resultCode, data);
}
答案 1 :(得分:0)
In the parent Activity class, override the onActivityResult() method and even override the same in the Fragment class and call as the following code.
In the parent class:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.dualPane);
fragment.onActivityResult(requestCode, resultCode, data);
}
In the child class:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// In fragment class callback
}