ProfileActivity: Replace fragments instead of hiding
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.wireguard.android;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.os.Bundle;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
/**
|
||||
* Fragment that shows a placeholder message.
|
||||
*/
|
||||
|
||||
public class PlaceholderFragment extends Fragment {
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.placeholder_fragment, parent, false);
|
||||
}
|
||||
}
|
||||
@@ -8,10 +8,11 @@ import android.view.MenuItem;
|
||||
* Base class for activities that use ProfileListFragment and ProfileDetailFragment.
|
||||
*/
|
||||
|
||||
class ProfileActivity extends ServiceClientActivity<ProfileServiceInterface> {
|
||||
abstract class ProfileActivity extends ServiceClientActivity<ProfileServiceInterface> {
|
||||
public static final String KEY_PROFILE_NAME = "profile_name";
|
||||
protected static final String TAG_DETAIL = "detail";
|
||||
protected static final String TAG_LIST = "list";
|
||||
protected static final String TAG_PLACEHOLDER = "placeholder";
|
||||
|
||||
private String currentProfile;
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.wireguard.android;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.os.Bundle;
|
||||
|
||||
/**
|
||||
* Activity that allows viewing information about a single WireGuard profile.
|
||||
*/
|
||||
|
||||
public class ProfileDetailActivity extends ProfileActivity {
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.profile_detail_activity);
|
||||
setTitle(getCurrentProfile());
|
||||
Fragment detailFragment = getFragmentManager().findFragmentByTag(TAG_DETAIL);
|
||||
((ProfileDetailFragment) detailFragment).setProfile(getCurrentProfile());
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,8 @@
|
||||
package com.wireguard.android;
|
||||
|
||||
import android.app.Fragment;
|
||||
import android.app.FragmentManager;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
|
||||
/**
|
||||
@@ -13,56 +12,55 @@ import android.os.Bundle;
|
||||
public class ProfileListActivity extends ProfileActivity {
|
||||
private boolean isSplitLayout;
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
final FragmentManager fm = getFragmentManager();
|
||||
if (fm.getBackStackEntryCount() > 0) {
|
||||
fm.popBackStack();
|
||||
} else {
|
||||
super.onBackPressed();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
// Set up the base layout and fill it with fragments.
|
||||
setContentView(R.layout.profile_list_activity);
|
||||
final int orientation = getResources().getConfiguration().orientation;
|
||||
isSplitLayout = orientation == Configuration.ORIENTATION_LANDSCAPE;
|
||||
updateLayout(getCurrentProfile());
|
||||
isSplitLayout = findViewById(R.id.fragment_container) != null;
|
||||
if (!isSplitLayout) {
|
||||
// Avoid ProfileDetailFragment adding its menu when it is not in the view hierarchy.
|
||||
final Fragment fragment = getFragmentManager().findFragmentByTag(TAG_DETAIL);
|
||||
if (fragment != null) {
|
||||
final FragmentTransaction transaction = getFragmentManager().beginTransaction();
|
||||
transaction.remove(fragment);
|
||||
transaction.commit();
|
||||
}
|
||||
}
|
||||
onProfileSelected(getCurrentProfile());
|
||||
}
|
||||
|
||||
public void onProfileSelected(String profile) {
|
||||
updateLayout(profile);
|
||||
setCurrentProfile(profile);
|
||||
if (isSplitLayout) {
|
||||
updateLayout(profile);
|
||||
setCurrentProfile(profile);
|
||||
} else if (profile != null) {
|
||||
final Intent intent = new Intent(this, ProfileDetailActivity.class);
|
||||
intent.putExtra(KEY_PROFILE_NAME, profile);
|
||||
startActivity(intent);
|
||||
setCurrentProfile(null);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateLayout(String profile) {
|
||||
final FragmentManager fm = getFragmentManager();
|
||||
final Fragment detailFragment = fm.findFragmentByTag(TAG_DETAIL);
|
||||
final Fragment listFragment = fm.findFragmentByTag(TAG_LIST);
|
||||
final FragmentTransaction transaction = fm.beginTransaction();
|
||||
public void updateLayout(String profile) {
|
||||
final Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment_container);
|
||||
if (profile != null) {
|
||||
if (isSplitLayout) {
|
||||
if (listFragment.isHidden())
|
||||
transaction.show(listFragment);
|
||||
if (fragment instanceof ProfileDetailFragment) {
|
||||
final ProfileDetailFragment detailFragment = (ProfileDetailFragment) fragment;
|
||||
detailFragment.setProfile(profile);
|
||||
} else {
|
||||
transaction.hide(listFragment);
|
||||
final ProfileDetailFragment detailFragment = new ProfileDetailFragment();
|
||||
detailFragment.setProfile(profile);
|
||||
final FragmentTransaction transaction = getFragmentManager().beginTransaction();
|
||||
transaction.replace(R.id.fragment_container, detailFragment, TAG_DETAIL);
|
||||
transaction.commit();
|
||||
}
|
||||
if (detailFragment.isHidden())
|
||||
transaction.show(detailFragment);
|
||||
} else {
|
||||
if (isSplitLayout) {
|
||||
if (detailFragment.isHidden())
|
||||
transaction.show(detailFragment);
|
||||
} else {
|
||||
transaction.hide(detailFragment);
|
||||
if (!(fragment instanceof PlaceholderFragment)) {
|
||||
final PlaceholderFragment placeholderFragment = new PlaceholderFragment();
|
||||
final FragmentTransaction transaction = getFragmentManager().beginTransaction();
|
||||
transaction.replace(R.id.fragment_container, placeholderFragment, TAG_PLACEHOLDER);
|
||||
transaction.commit();
|
||||
}
|
||||
if (listFragment.isHidden())
|
||||
transaction.show(listFragment);
|
||||
}
|
||||
transaction.commit();
|
||||
((ProfileDetailFragment) detailFragment).setProfile(profile);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user