Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to call `overridePendingTransition` from a class that does not extend the Activity class?

I'm using another class to run some stuff in the background while the main activity is being displayed, and passing that activity's context to this background class. I'm starting another activity from this background class, but am unable to call overridePendingTransition here because "method overridePendingTransition(int, int) is undefined for the type BackgroundClass."

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) 
    {
        ...
    }



}
like image 959
Tim Avatar asked Mar 12 '14 14:03

Tim


1 Answers

So I actually was able to solve the problem by calling overridePendingTransition on the context, casted to an Activity.

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

I realize that this is not the best practice and could get messy with a more complex application, but for our purposes right now I think this is ok. I would like to investigate @bariscan Kayaoglu's solution eventually, as it seems more robust.

like image 182
Tim Avatar answered Oct 10 '22 04:10

Tim