从backstack恢复Android给我空白片段

时间:2014-07-03 12:39:18

标签: android android-fragments

嗨我有背斜问题。这是我的片段列表: A - 仪表板碎片 B - NewOrders Fragment C - 产品碎片

当我导航A - >时,Backstack正在工作B(背压) - > A - 这没关系

但在这种情况下A - > B - > C(背压) - > B(空白片段)(背面压制) - > A(空白片段)

仪表板碎片:

public class DashboardFragment extends Fragment implements View.OnClickListener{
public static final String TAG = DashboardFragment.class.getSimpleName();
ImageButton scan;
ImageButton paragon;
ImageButton cart;
ImageButton orders;

public DashboardFragment() { }

public static DashboardFragment newInstance(){
    return new DashboardFragment();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.d(TAG, "Fragment active: ************************************************************");
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_dashboard, container, false);
    scan = (ImageButton) rootView.findViewById(R.id.scanButton);
    paragon = (ImageButton) rootView.findViewById(R.id.paragonButton);
    cart = (ImageButton) rootView.findViewById(R.id.cartButton);
    orders = (ImageButton) rootView.findViewById(R.id.newOrdersButton);
    scan.setOnClickListener(this);
    paragon.setOnClickListener(this);
    cart.setOnClickListener(this);
    orders.setOnClickListener(this);
    return rootView;
}


@Override
public void onClick(View v) {
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    switch(v.getId()){
        case R.id.paragonButton:
            ft.replace(R.id.container, ReceiptFragment.newInstance());
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            break;
        case R.id.scanButton:
            ft.replace(R.id.container, ProductsFragment.newInstance());
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            break;
        case  R.id.cartButton:
            ft.replace(R.id.container, CartFragment.newInstance());
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            break;
        case R.id.newOrdersButton:
            ft.replace(R.id.container, NewOrdersFragment.newInstance());
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            break;
        default:
            return;
    }
    ft.addToBackStack(null);
    ft.commit();
}
}

NewOrdersFragment:

public class NewOrdersFragment extends Fragment implements ExpandableListView.OnChildClickListener{
private static final String TAG = NewOrdersFragment.class.getSimpleName();
List<JsonNewOrder> newOrderList;
ExpandableListView listView;

public NewOrdersFragment() {}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.d(TAG, "Fragment active: ************************************************************");
    View rootView = inflater.inflate(R.layout.fragment_new_orders, container, false);
    newOrderList = new ArrayList<>();
    listView = (ExpandableListView) rootView.findViewById(R.id.expandableListView);
    listView.setOnChildClickListener(this);
    listView.setAdapter(new ExpandableListAdapter(getActivity(), newOrderList));
    return rootView;
}


public static Fragment newInstance() {
    return new NewOrdersFragment();
}

@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
    Long productId = ((JsonOrder)parent.getExpandableListAdapter().getChild(groupPosition, childPosition)).getBarcode();
    ProductFragment fragment = ProductFragment.newInstance(String.valueOf(productId));
    FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
    ft.replace(R.id.container, fragment);
    ft.addToBackStack(null);
    ft.commit();
    return true;
}

@Override
public void onResume() {
    super.onResume();
    if(NetworkHelper.isConnected(getActivity())) new JSONTask().execute();
}

private class JSONTask extends AsyncTask<Void, Void, String>{
    ....
}
}

ProductFragment:

public class ProductFragment extends Fragment {
private static final String ARG_EAN = "ean";
private static final String TAG = ProductFragment.class.getSimpleName();
ImageView thumbnail;
private String ean;

public static ProductFragment newInstance(String ean) {
    ProductFragment fragment = new ProductFragment();
    Bundle args = new Bundle();
    args.putString(ARG_EAN, ean);
    fragment.setArguments(args);
    return fragment;
}

public ProductFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        ean = getArguments().getString(ARG_EAN);
    }
}

private void setImage(Bitmap image){
    thumbnail.setImageBitmap(image);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.d(TAG, "Fragment active: ************************************************************");
    View rootView = inflater.inflate(R.layout.fragment_product, container, false);
    thumbnail = (ImageView) rootView.findViewById(R.id.thumbnail);
    TextView gender = (TextView) rootView.findViewById(R.id.gender_text);
    TextView category = (TextView) rootView.findViewById(R.id.category_text);
    TextView name = (TextView) rootView.findViewById(R.id.name_text);
    TextView size = (TextView) rootView.findViewById(R.id.size_text);
    TextView color = (TextView) rootView.findViewById(R.id.color_text);
    ImageView color_thumb = (ImageView) rootView.findViewById(R.id.color_thumbnail);
    TextView price = (TextView) rootView.findViewById(R.id.price_text);
    String [] projection = Product.getProjection();
    String selection = Product.C_ID + "=?";
    String [] selectionArgs = {ean};
    Cursor cursor = getActivity().getContentResolver().query(Uri.parse(Product.CONTENT_URI + "/" + ean), projection, selection, selectionArgs, null);
    if(cursor.moveToFirst()) {
        if (!cursor.isNull(cursor.getColumnIndex(Product.C_GENDER_NAME)))
            gender.setText(cursor.getString(cursor.getColumnIndex(Product.C_GENDER_NAME)));
        if (!cursor.isNull(cursor.getColumnIndex(Product.C_CATEGORY_NAME)))
            category.setText(cursor.getString(cursor.getColumnIndex(Product.C_CATEGORY_NAME)));
        if (!cursor.isNull(cursor.getColumnIndex(Product.C_PRODUCT_NAME)))
            name.setText(cursor.getString(cursor.getColumnIndex(Product.C_PRODUCT_NAME)));
        if (!cursor.isNull(cursor.getColumnIndex(Product.C_PRICE)))
            price.setText(cursor.getString(cursor.getColumnIndex(Product.C_PRICE)) + " PLN");
        if (!cursor.isNull(cursor.getColumnIndex(Product.C_COLOR_NAME)))
            color.setText(cursor.getString(cursor.getColumnIndex(Product.C_COLOR_NAME)));
        if (!cursor.isNull(cursor.getColumnIndex(Product.C_COLOR_HEX)))
            color_thumb.setBackgroundColor(Color.parseColor(cursor.getString(cursor.getColumnIndex(Product.C_COLOR_HEX))));
        if (!cursor.isNull(cursor.getColumnIndex(Product.C_SIZE_NAME)))
            size.setText(cursor.getString(cursor.getColumnIndex(Product.C_SIZE_NAME)));

        String [] stockProjection = {Stock.T_NAME + "." + Stock.C_ID, Stock.T_NAME + "." + Stock.C_NAME, ProductsStocks.T_NAME + "." + ProductsStocks.C_AMOUNT};
        Cursor stockCursor= getActivity().getContentResolver().query(Uri.parse(ProductsStocks.CONTENT_URI + "/" + cursor.getString(0)), stockProjection, null, null, null);
        if(cursor.moveToFirst()){
            ListView listView = (ListView) rootView.findViewById(R.id.stock_list);
            String [] from = {Stock.C_NAME, ProductsStocks.C_AMOUNT};
            int[] to = {R.id.row_stock_name, R.id.row_stock_qty};
            listView.setAdapter(new SimpleCursorAdapter(getActivity(), R.layout.row_stock_item, stockCursor, from, to));
        }
        //stockCursor.close();
        if(NetworkHelper.isConnected(getActivity())) new DownloadImage().execute(cursor.getString(cursor.getColumnIndex(Product.C_THUMB_URI)));

    }else{
        getFragmentManager().beginTransaction().replace(R.id.productContainer, new NoProductFragment()).commit();
    }
    cursor.close();
    return rootView;
}

1 个答案:

答案 0 :(得分:2)

我假设您已经正确处理onBackPressed()并使用popBackStack()方法跳回到上一个片段。

片段永远不应该替换自己。你总会遇到问题。相反,告诉活动您想要被另一个片段替换。像这样:

interface AppEventListener{
    void onNewOrdersSelected();
    void onDashboardSelected();
}

class MainActivity extends Activity implements AppEventListener{
    ...
    void onNewOrdersSelected(){
        //replace fragment with NewOrders fragment
    }
    void onDashBoardSelected(){
        //replace fragment with Dashboard fragment
    }
    ...

    @Override
    public void onBackPressed() {
        FragmentManager manager = getFragmentManager();
        int count = manager.getBackStackEntryCount();

        if(count==0) {
            super.onBackPressed();
        }else{
            manager.popBackStack();
        }
    }

}

class DashBoardFragment extends Fragment{
    ...
    public void OnClick(View view){
        AppEventListener listener = (AppEventListener) getActivity();
        ...
        case(R.id.NewOrdersButton):
            listener.onNewOrdersSelected();
        ...
    }
}