简体   繁体   中英

How to override the transition from an activity's context?

I want to override the transition from page to page from the following: In the current Activity, I'm running another class's methods in the background using my current activity's context. From that class I open up 1 of 2 possible activities (Using the context, but not directly through my initial activity). Is there a way to override the page transition from the initial activity to both new activities? Because overridePendingTransition can only be run on an activity object, I cannot just call this method in both cases, as the cases are not part of the actual activity.

Sorry if this isn't so clear, feel free to ask questions that will clear up anything. I'm still not exactly 100% on definitions of intent and context.

//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);
                        context.overridePendingTransition(R.layout.fade_in, R.layout.fade_out);

                    }
                    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);
                        context.overridePendingTransition(R.layout.fade_in, R.layout.fade_out);
                    }

I tried using context.overridePendingTransition... but that cannot be done on a context. However the following bit of code works when run from another Activity, not a context item:

startActivity(letsMenu);
        overridePendingTransition(R.layout.fade_in, R.layout.fade_out);

只需将上下文包装如下:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM