アンドロイドアプリで作業しているときに、開いているときと閉じているときにナビゲーションドロワーがハングすることがわかりました。それは2〜3秒間かかります。ほとんどのデバイスでは大丈夫です(主にAndroid搭載)が、一部のデバイスではハングします(主にSamsung)。いくつかのデバイスで開閉中にドロワーが掛かってしまう
Home.java(引き出しが実施されるアクティビティ):
public class Home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
Fragment fragment;
public void MsgBox(String title, String msg,int id){
new LovelyInfoDialog(this)
.setTopColorRes(R.color.primary)
.setIcon(R.drawable.ic_info_white_24dp)
//This will add Don't show again checkbox to the dialog. You can pass any ID as argument
.setNotShowAgainOptionEnabled(id)
.setTitle(title)
.setMessage(msg)
.show();
}
public boolean showAds() {
SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean showAd = getPrefs.getBoolean("showAd", true);
return showAd;
}
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
hasPermissions(this,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.GET_ACCOUNTS);
int PERMISSION_ALL = 1;
String[] PERMISSIONS = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.GET_ACCOUNTS};
if(!hasPermissions(this, PERMISSIONS)){
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
AppRate.with(this)
.setInstallDays(5) // default 10, 0 means install day.
.setLaunchTimes(10) // default 10
.setRemindInterval(2) // default 1
.setShowLaterButton(true) // default true
.setDebug(false) // default false
.monitor();
AppRate.showRateDialogIfMeetsConditions(this);
final NavigationView navigationView = (NavigationView) findViewById(nav_view);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
navigationView.getMenu().getItem(1).setChecked(true);
onNavigationItemSelected(navigationView.getMenu().getItem(1));
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
fragment=new QuestionPap();
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment, "Home");
ft.commit();
}
}
boolean doubleBackToExitPressedOnce = false;
boolean drawe=false;
void Back(){
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
this.drawe=true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
doubleBackToExitPressedOnce=false;
drawe=true;
}
}, 2000);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (!drawe) {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
if (fragment == getSupportFragmentManager().findFragmentByTag("Home")) {
super.onBackPressed();
} else {
drawer.openDrawer(GravityCompat.START);
Back();
}
}
}else{
super.onBackPressed();
}
}
@Override
protected void onDestroy(){
super.onDestroy();
Intent i = new Intent(this,DownloadService.class);
stopService(i);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
try {
int id = item.getItemId();
// fragment = null;
String TAG=null;
AppBarLayout appbar = (AppBarLayout)findViewById(R.id.appbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
if (id == R.id.home && fragment!=getSupportFragmentManager().findFragmentByTag("Home")) {
TAG="Home";
fragment = new QuestionPap();
fab.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appbar.setTranslationZ(0);
}
} else if (id == R.id.offline && fragment!=getSupportFragmentManager().findFragmentByTag("Offline")) {
TAG="Offline";
MsgBox("Save Files Offline","Tap and hold any file to delete it.",2);
fragment = new offline();
fab.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appbar.setTranslationZ(6*2);
}
} else if (id == R.id.syll && fragment!=getSupportFragmentManager().findFragmentByTag("Syllabus")) {
TAG="Syllabus";
fragment = new Syllabus();
fab.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appbar.setTranslationZ(0);
}
} else if (id == R.id.Tools && fragment!=getSupportFragmentManager().findFragmentByTag("Tools")) {
TAG="Tools";
fragment = new Tools();
fab.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appbar.setTranslationZ(6*2);
}
} else if (id == R.id.contribute && fragment!=getSupportFragmentManager().findFragmentByTag("Contribute")) {
TAG="Contribute";
fragment = new contribute();
fab.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appbar.setTranslationZ(6*2);
}
} else if (id == R.id.about && fragment!=getSupportFragmentManager().findFragmentByTag("About")) {
TAG="About";
fragment = new about();
fab.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appbar.setTranslationZ(6*2);
}
} else if (id==R.id.notices && fragment!=getSupportFragmentManager().findFragmentByTag("Notices")) {
TAG="Notices";
fragment = new notices();
fab.setVisibility(View.GONE);
MsgBox("Get Latest News", "Don't miss out on any department news.",3);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appbar.setTranslationZ(6*2);
}
} else if (id==R.id.donate && fragment!=getSupportFragmentManager().findFragmentByTag("Donate")) {
TAG="Donate";
fragment = new donate();
fab.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appbar.setTranslationZ(6 * 2);
}
} else if (id==R.id.assig && fragment!=getSupportFragmentManager().findFragmentByTag("Assignments")) {
TAG = "Assignments";
fragment = new assignments();
fab.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
appbar.setTranslationZ(6 * 2);
}
}
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment,TAG);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
catch (Exception ex){
}
return true;
}
}
私はその背後にある理由だか分かりません。私がそれを開こうとすると、それはハングアップします。オプションをクリックしてフラグメントを開くと、フラグメントが読み込まれていないときに引出しがハングするため、フラグメントの読み込みとは関係ないと思う。他のコードが必要な場合は、私はそれを表示する準備ができています。