overridePendingTransition

overridePendingTransition

我正在使用另一个类在显示主活动时在后台运行一些东西,并将该活动的上下文传递给这个后台类。我正在从这个后台类启动另一个活动,但是无法在这里调用overridePendingTransition,因为“方法overridependingtransition(int,int)对于类型background class是未定义的。”

public class GetUPC extends AsyncTask<Void, Void, Void>
{
    @Override
    protected void onPreExecute()
    {
        ...
    }
    @Override
    protected Void doInBackground(Void... arg0)
    {
        ...


                    boolean dairy;
                    if(theDairy.equals("N"))
                    {
                        //milk test
                        dairy=true;
                    }
                    else
                    {
                        dairy=false;
                    }



                    //depending on if there is a warning it will either display the warning screen or skip it
                    if(dairy)
                    {
                        Intent intent_warn = new Intent(context, WarningScreen.class);

                        intent_warn.putExtra("Name", str_name);
                        intent_warn.putExtra("Size", str_size);
                        intent_warn.putExtra("Price", str_price);
                        intent_warn.putExtra("Carbs", str_carbs);
                        intent_warn.putExtra("Protein", str_protein);
                        intent_warn.putExtra("Fiber", str_fiber);
                        intent_warn.putExtra("Sugar", str_sugar);
                        intent_warn.putExtra("SatFat", str_satFat);
                        intent_warn.putExtra("TotFat", str_totFat);
                        intent_warn.putExtra("Cholesterol", str_cholesterol);
                        intent_warn.putExtra("Sodium", str_sodium);
                        intent_warn.putExtra("Potassium", str_potassium);
                        intent_warn.putExtra("Calories", str_calories);
                        intent_warn.putExtra("Warning", "Contains Dairy");
                        intent_warn.putExtra("WarningRed", true);
                        Log.e("Warning",intent_warn.getExtras().getString("Warning"));

                        context.startActivity(intent_warn);
                        overridePendingTransition(R.layout.fade_in, R.layout.fade_out);  //THIS PART ISN'T WORKING//
                    }
                    else
                    {
                        Intent intent_menu = new Intent(context, DisplayScreen.class);
                        intent_menu.putExtra("Name", str_name);
                        intent_menu.putExtra("Size", str_size);
                        intent_menu.putExtra("Price", str_price);
                        intent_menu.putExtra("Carbs", str_carbs);
                        intent_menu.putExtra("Protein", str_protein);
                        intent_menu.putExtra("Fiber", str_fiber);
                        intent_menu.putExtra("Sugar", str_sugar);
                        intent_menu.putExtra("SatFat", str_satFat);
                        intent_menu.putExtra("TotFat", str_totFat);
                        intent_menu.putExtra("Cholesterol", str_cholesterol);
                        intent_menu.putExtra("Sodium", str_sodium);
                        intent_menu.putExtra("Potassium", str_potassium);
                        intent_menu.putExtra("Calories", str_calories);
                        intent_menu.putExtra("Warning",  "Contains no allergens");
                        intent_menu.putExtra("WarningRed", false);
                        Log.e("Warning",intent_menu.getExtras().getString("Warning"));

                        context.startActivity(intent_menu);
                    }


                    Log.e("KYLE_DATA_UPCH",str_name+" "+str_price+""+str_size);
                }
            }
                catch (JSONException e)
                {
                    e.printStackTrace();
                }

            }
            else
            {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
                _errorCode=3;
            }

            return null;
        }
    @Override
    protected void onPostExecute(Void result)
    {
        ...
    }



}

最佳答案

所以我实际上能够通过在上下文中调用overridePendingTransition来解决问题,并将其转换为一个活动。

((Activity) context).overridePendingTransition(R.layout.fade_in, R.layout.fade_out);

我意识到这不是最佳实践,可能会让更复杂的应用程序变得混乱,但就我们现在的目的而言,我认为这是可以的。我想最终研究@bariscan kayaoglu的解决方案,因为它看起来更健壮。

07-24 09:49