如何在活动和片段上正确使用后栈

时间:2016-04-16 11:00:30

标签: android android-fragments back-stack fragment-backstack

我有2个活动: - LoginActivity:它将在以后处理登录内容 - MainActivity:主应用程序,所有功能都将在片段上提供。

我想正确使用后台,但我不能。

我想要(但不能)实现的目标: 应用程序从登录屏幕开始。我登录(现在按下“开始”按钮),然后出现主屏幕并加载第一个片段。 现在,如果我按下后退按钮,应用程序将关闭,这就是它应该如何工作,它不能通过简单的后退按钮返回到登录屏幕。

但是现在如果我点击退出(并显示登录屏幕),然后我按下后退按钮,应用程序会将我带回主屏幕,这真的不合适。如果我退出,则从登录屏幕返回按钮应该关闭应用程序。

另一个问题,关于backstack和片段: 我有3个片段:第一,第二和第三。如果我在彼此之后打开它们(当然是在主要活动中),然后按下后退按钮,我希望它将我带回到之前打开的片段,或者如果之前没有,请关闭应用程序。 现在它创建它,打开上一个片段,但同时它关闭了完整的应用程序。 (我可以看到他们正在发生在彼此之后)

缺少什么?应该修改什么来实现这种(正常)行为?

谢谢!

我的代码:

MainActivity:

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

public static final String SP_DATA = "SP_DATA";
public static final String FB_LOGIN_STATUS = "FB_LOGIN_STATUS";
public static final String FLRT = "FLRT";

public SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {

    sharedPreferences = getSharedPreferences(SP_DATA, MODE_PRIVATE);
    boolean bAlreadyLoggedIn = sharedPreferences.getBoolean(FB_LOGIN_STATUS, false);

    if (bAlreadyLoggedIn) {
        Log.d(FLRT, "Already logged in");
    }
    else {
        Log.d(FLRT, "Not logged in");

        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        startActivity(intent);
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    showFragment(new FirstFragment(),"FirstFragment");

    Button btnLogout = (Button) findViewById(R.id.btnLogout);

    btnLogout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            logout();

            showActivity(MainActivity.this, LoginActivity.class);
        }
    });

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    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.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

private void showActivity(Context context_fromActivity, Class<?> class_toActivty){
    Intent intent = new Intent(context_fromActivity, class_toActivty);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}

private void logout() {
    Log.d(FLRT, "Logging out...");

    sharedPreferences = getSharedPreferences(SP_DATA, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(FB_LOGIN_STATUS, false);
    editor.commit();
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }

    if (getFragmentManager().getBackStackEntryCount() > 0 ){
        getFragmentManager().popBackStack();
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_first_layout) {
        showFragment(new FirstFragment(), "FirstFragment");
    }
    else if (id == R.id.nav_second_layout) {
        showFragment(new SecondFragment(), "SecondFragment");
    }
    else if (id == R.id.nav_third_layout) {
        showFragment(new ThirdFragment(), "ThirdFragment");
    }
    else if (id == R.id.nav_share) {

    }
    else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

private void showFragment(Fragment fragment, String sFragmentTAG) {
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    if (fragmentManager.findFragmentByTag(sFragmentTAG) != null) {
        Log.d(FLRT, "Fragment found, using existing one: " + sFragmentTAG);
        fragment = fragmentManager.findFragmentByTag(sFragmentTAG);
    }
        fragmentTransaction.replace(R.id.fragmentContainer, fragment, sFragmentTAG);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

}
}

LoginActivity:

public class LoginActivity extends AppCompatActivity {

public static final String SP_DATA = "SP_DATA";
public static final String FB_LOGIN_STATUS = "FB_LOGIN_STATUS";
public static final String FLRT = "FLRT";

public SharedPreferences sharedPreferences;

private Button btn_start;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    btn_start = (Button) findViewById(R.id.btn_start);

    btn_start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            login();

            showActivity(LoginActivity.this, MainActivity.class);;
        }
    });
}

private void showActivity(Context context_fromActivity, Class<?> class_toActivty){
    Intent intent = new Intent(context_fromActivity, class_toActivty);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
}

private void login() {
    Log.d(FLRT, "Logging in...");

    sharedPreferences = getSharedPreferences(SP_DATA, MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putBoolean(FB_LOGIN_STATUS, true);
    editor.commit();
}
}

1 个答案:

答案 0 :(得分:1)

关于在Activities之间切换。

一件重要的事情。
showActivity()时使用finish()来转移到另一个时杀死当前Activity。如果它没有帮助 - 见下文。

尝试同时使用FLAG_ACTIVITY_NEW_TASK当前的标记,因为FLAG_ACTIVITY_CLEAR_TOP只会关闭当前活动以上任务中的活动,但不会关闭。 所以,就是这样:intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);

另一种选择是仅使用:FLAG_ACTIVITY_CLEAR_TASK - 新活动将清除任务并成为其中的根。

如果两者都没有帮助 - 在每份android:launchMode = "singleInstance"的清单中使用Actitivy



关于Fragments - 我在您的代码中看到了一个小错误:它在super.onBackPressed(); Fragment中弹出BackStack之前调用了@Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else if (getFragmentManager().getBackStackEntryCount() > 0 ){ getFragmentManager().popBackStack(); } else { super.onBackPressed(); } } 我会这样改变:

getFragmentManager().getBackStackEntryCount() > 0

!! 此外,您应该测试此行:0条件1FragmentContainer条件(因为在我的项目{{1}中的当前实现中}首先考虑BackStack中的零值。只需测试0和1即可查看哪些有效。

感谢。


PS。更多提示。

您可以在FragmentManager fragmentManager中声明Activity,以便仅在onCreate()中创建一次if (id == R.id.nav_first_layout) { showFragment(new FirstFragment(), "FirstFragment"); } else if (id == R.id.nav_second_layout) { showFragment(new SecondFragment(), "SecondFragment"); } else if (id == R.id.nav_third_layout) { showFragment(new ThirdFragment(), "ThirdFragment"); } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } 。这比每次都更好。(
考虑改变这个:

switch (item.getItemId()) {
    case R.id.nav_first_layout:
        showFragment(new FirstFragment(), "FirstFragment");
        break;
    case ...
}

要:

<com.example.myapplication.CustomTextView
            android:id="@+id/tvScrollingMessage"
            android:text="@string/scrolling_message_main_wish_list"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:marqueeRepeatLimit ="marquee_forever"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:scrollHorizontally="true"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="@color/black"
            android:gravity="center"
            android:textColor="@color/white"
            android:textSize="15dp"
            android:freezesText="true"/>

等等。代码会更漂亮:))