gpt4 book ai didi

android - 有没有办法从不扩展 Activity 类的类中调用 `overridePendingTransition`?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:19:32 26 4
gpt4 key购买 nike

我正在使用另一个类在显示主要 Activity 时在后台运行一些东西,并将该 Activity 的上下文传递给这个后台类。我正在从此后台类开始另一个 Activity ,但无法在此处调用 overridePendingTransition,因为“方法 overridePendingTransition(int, int) 未针对 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)
{
...
}



}

最佳答案

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

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

我意识到这不是最佳实践,并且可能会弄乱更复杂的应用程序,但为了我们现在的目的,我认为这没问题。我最终想研究@bariscan Kayaoglu 的解决方案,因为它看起来更可靠。

关于android - 有没有办法从不扩展 Activity 类的类中调用 `overridePendingTransition`?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22354076/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com