如何在父活动的简历上维护多个片段

时间:2015-02-21 11:45:55

标签: android android-fragments callback

我正在使用导航抽屉,在点击导航抽屉的列表项时会调用多个片段

在我的场景中,父活动处理2个片段。我在处理这些片段时遇到了预期的行为。

这是我正在做的事情:

转到offline mode - >转到fragment 2 - > Minimize the app - >转到online mode(在互联网上切换) - > Resume the app

应用程序的

On resume,调用父活动,在父活动中,默认情况下会打开片段1,而不应打开片段2.

帮助表示赞赏。

这是我的导航抽屉代码 -

public class NavigationDrawer extends ActionBarActivity implements OnItemClickListener {

private ActionBar actionBar;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
RelativeLayout user_panel;

LinearLayout mylinearlayout;
private ActionBarDrawerToggle mDrawerToggle;

// Initialize SharedPreferences and editor variable's.
SharedPreferences.Editor editor;
SharedPreferences pref;

String username;

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

    // Initialize SharedPreferences mode
    pref = getApplicationContext().getSharedPreferences("MyPref", 0);
    editor = pref.edit();
    username = pref.getString("name", null); // getting String



    moveDrawerToTop();
    initActionBar();
    initDrawer();
    //Quick cheat: Add Fragment 1 to default view
    onItemClick(null, null, 0, 0);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

private void moveDrawerToTop() {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    DrawerLayout drawer = (DrawerLayout) inflater.inflate(R.layout.decor, null); // "null" is important.

    // HACK: "steal" the first child of decor view
    ViewGroup decor = (ViewGroup) getWindow().getDecorView();
    View child = decor.getChildAt(0);
    decor.removeView(child);
    LinearLayout container = (LinearLayout) drawer.findViewById(R.id.drawer_content); // This is the container we defined just now.
    container.addView(child, 0);
    drawer.findViewById(R.id.drawer).setPadding(0, getStatusBarHeight(), 0, 0);
    ImageView profile = (ImageView) drawer.findViewById(R.id.image_view);
    profile.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    // Make the drawer replace the first child
    decor.addView(drawer);
}

public int getStatusBarHeight() {
    int result = 0;
    int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
    if (resourceId > 0) {
        result = getResources().getDimensionPixelSize(resourceId);
    }
    return result;
}

private int getContentIdResource() {
    return getResources().getIdentifier("content", "id", "android");
}

@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.
    mDrawerToggle.syncState();
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    int id = item.getItemId();
    if (id == R.id.notification) {

        Intent display_notify = new Intent(getApplicationContext(), DisplayActivity.class);
        startActivity(display_notify);

        return true;
    }
    return super.onOptionsItemSelected(item);
}

private void initActionBar() {
    actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setHomeButtonEnabled(true);
}

private void initDrawer() {
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mylinearlayout = (LinearLayout) findViewById(R.id.drawer);
    mDrawerLayout.setDrawerListener(createDrawerToggle());
    mDrawerList = (ListView) findViewById(R.id.MyList);
    user_panel = (RelativeLayout) findViewById(R.id.user_profile_panel);
    user_panel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    TextView user_name = (TextView) findViewById(R.id.left_panel_user_name);
    user_name.setText(username);

    ListAdapter adapter = (ListAdapter) (new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.nav_items)));
    mDrawerList.setAdapter(adapter);
    mDrawerList.setOnItemClickListener(this);
}

private DrawerListener createDrawerToggle() {
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerClosed(View view) {
            super.onDrawerClosed(view);
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
        }

        @Override
        public void onDrawerStateChanged(int state) {
        }
    };
    return mDrawerToggle;
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    mDrawerLayout.closeDrawer(mylinearlayout);
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction ftx = fragmentManager.beginTransaction();
    if (position == 0) {
        ftx.replace(R.id.main_content, new FragmentOne());
    }  else if (position == 1) {
        ftx.replace(R.id.main_content, new FragmentSecond());
    } else if (position == 2) {

    } else if (position == 3) {

    } else if (position == 4) {
        invitation();
    } else if (position == 5) {
        launchMarket();
    } else if (position == 6) {
        ftx.replace(R.id.main_content, new Setting_Fragment());
    }
    ftx.commit();
}

}

0 个答案:

没有答案