fix fragment management in AppSettingsActivity

This commit is contained in:
Vincent Breitmoser
2018-06-21 13:31:03 +02:00
parent aa596e0165
commit 651e799fe2

View File

@@ -28,9 +28,9 @@ import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
@@ -48,8 +48,6 @@ import timber.log.Timber;
public class AppSettingsActivity extends BaseActivity { public class AppSettingsActivity extends BaseActivity {
private Uri mAppUri; private Uri mAppUri;
private AppSettingsAllowedKeysListFragment mAllowedKeysFragment;
private TextView mAppNameView; private TextView mAppNameView;
private ImageView mAppIconView; private ImageView mAppIconView;
@@ -64,19 +62,9 @@ public class AppSettingsActivity extends BaseActivity {
mAppNameView = findViewById(R.id.api_app_settings_app_name); mAppNameView = findViewById(R.id.api_app_settings_app_name);
mAppIconView = findViewById(R.id.api_app_settings_app_icon); mAppIconView = findViewById(R.id.api_app_settings_app_icon);
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() { findViewById(R.id.fab).setOnClickListener(v -> startApp());
@Override
public void onClick(View v) {
startApp();
}
});
setFullScreenDialogClose(new View.OnClickListener() { setFullScreenDialogClose(v -> cancel());
@Override
public void onClick(View v) {
cancel();
}
});
setTitle(null); setTitle(null);
Intent intent = getIntent(); Intent intent = getIntent();
@@ -92,7 +80,19 @@ public class AppSettingsActivity extends BaseActivity {
} }
private void save() { private void save() {
mAllowedKeysFragment.saveAllowedKeys(); FragmentManager supportFragmentManager = getSupportFragmentManager();
if (supportFragmentManager == null) {
Timber.e("Could not retrieve fragmentmanager for saving!");
return;
}
AppSettingsAllowedKeysListFragment allowedKeysFragment = (AppSettingsAllowedKeysListFragment)
supportFragmentManager.findFragmentById(R.id.api_allowed_keys_list_fragment);
if (allowedKeysFragment == null) {
Timber.e("Could not retrieve fragment for saving!");
return;
}
allowedKeysFragment.saveAllowedKeys();
setResult(Activity.RESULT_OK); setResult(Activity.RESULT_OK);
finish(); finish();
} }
@@ -199,12 +199,11 @@ public class AppSettingsActivity extends BaseActivity {
return; return;
} }
// Create an instance of the fragments AppSettingsAllowedKeysListFragment allowedKeysFragment = AppSettingsAllowedKeysListFragment.newInstance(allowedKeysUri);
mAllowedKeysFragment = AppSettingsAllowedKeysListFragment.newInstance(allowedKeysUri);
// Add the fragment to the 'fragment_container' FrameLayout // Add the fragment to the 'fragment_container' FrameLayout
// NOTE: We use commitAllowingStateLoss() to prevent weird crashes! // NOTE: We use commitAllowingStateLoss() to prevent weird crashes!
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.replace(R.id.api_allowed_keys_list_fragment, mAllowedKeysFragment) .replace(R.id.api_allowed_keys_list_fragment, allowedKeysFragment)
.commitAllowingStateLoss(); .commitAllowingStateLoss();
// do it immediately! // do it immediately!
getSupportFragmentManager().executePendingTransactions(); getSupportFragmentManager().executePendingTransactions();