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

I am using another class to run some things in the background while the main activity is being displayed, and passing this activity context to this background class. I am launching another action from this background class, but I cannot call it overridePendingTransitionhere because "the overridePendingTransition (int, int) method has an undefined value for the BackgroundClass type".

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



}
+4
source share
5 answers

, , overridePendingTransition , Activity.

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

, , , . @bariscan Kayaoglu, .

+19

.

myInterface mInterface;
public interface myInterface {
    public abstract void myTask();
}

public GetUPC(myInterface mInterface) {
    this.mInterface = mInterface;
}

doInBackground, ,

mInterface.myTask();

async.

myAsyncTask = new GetUPC(this);

, myTask(). , , , .

+2

Tims, , Activity :

if (context instanceof Activity) {
  ((Activity) context).overridePendingTransition(R.layout.fade_in, R.layout.fade_out);
}

, , . try/catch, ,

+2

, . Async, , async . , .

0

You cannot do part of updating the user interface from a background thread, but if you want to do the same, override the onProgressUpdate class of the AsyncTask class and run the initial activity code in this. to invoke this publishProgress method call. Before you begin, you must cancel your AsyncTask, otherwise your application will click.

0
source

Source: https://habr.com/ru/post/1531347/


All Articles