回到片段时显示白屏?

时间:2017-05-04 08:27:26

标签: android android-fragments

如果我按回home片段然后显示白屏然后按应用程序完成,我在Container_activity Oncreate方法中调用home片段。我想当我按回home片段应用程序将关闭并且不显示空白屏幕我的代码是: -

public class Container_Activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    FrameLayout frameLayout;
    static Container_Activity activity;

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

        if (android.os.Build.VERSION.SDK_INT >= 21) {
            Window window = this.getWindow();

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.setStatusBarColor(this.getResources().getColor(R.color.statusbarcolor));

        }
        activity = this;

        Container_Activity.showFragment(new Home_fragment(), "");

        frameLayout = (FrameLayout) findViewById(R.id.container_frame);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        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);
    }

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

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

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


    public static void showFragment(Fragment fragment, String Tag) {
        FragmentManager manager = activity.getSupportFragmentManager();
        FragmentTransaction transcation = manager.beginTransaction();
        transcation.replace(R.id.container_frame, fragment, Tag);
        transcation.addToBackStack(null);
        transcation.commit();

    }
}

活动类: -

def get_param(string): #whole not splitted string
try:
    return dict(item.split('=') for item in string.split('&'))['a']
except:
    return string


from pyspark.sql.types import *
get_param_udf = udf(get_param, StringType())

new_data = some_data.withColumn('a', get_param_udf('string'))

3 个答案:

答案 0 :(得分:1)

修改

添加主片段时不要使用

transcation.addToBackStack(null);

对于除家之外的片段,您可以使用

transcation.addToBackStack(null);

showFragment()功能

中删除以下行
 transcation.addToBackStack(null);

答案 1 :(得分:1)

为您 要求 transcation.add 而不是 transcation.replace

fragmentTransaction.add(int containerViewId,Fragment fragment,String tag)

  

将片段添加到活动状态。该片段可以任选地   也有它的视图(如果Fragment.onCreateView返回非null)   容器视图的活动。

transcation.add(R.id.container_frame, fragment, Tag);

阅读 Difference between add(), replace()

答案 2 :(得分:0)

fragmentTransaction.addToBackStack()//不要为你的第一个片段包含这个.//

if(getSupportFragmentManager().getBackStackEntryCount() !=1){
                fragmentTransaction.addToBackStack("placeholder");
            }