[英]Share activity context among intents
I have 3 screens in my app, each of which are in their own classes.我的应用程序中有 3 个屏幕,每个屏幕都在自己的类中。 When the app launches, my Driver class sets up some GUI elements, and then launches the first Intent.
当应用程序启动时,我的驱动程序 class 设置了一些 GUI 元素,然后启动第一个 Intent。
I have a separate GUI
class (which Driver invokes) which handles everything from menu's to dialog boxes.我有一个单独的
GUI
class(驱动程序调用),它处理从菜单到对话框的所有内容。 Previously my app didn't use Intents
so I could pass the activity/context from Driver
to Gui
in its constructor as an object of type Activity
and as a result could define layouts etc like LinearLayout ll = new LinearLayout(activity)
and everything would be operating in the same activity/context.以前我的应用程序没有使用
Intents
,所以我可以在其构造函数中将Activity
LinearLayout ll = new LinearLayout(activity)
上下文从Driver
传递给Gui
在相同的活动/上下文中运行。
Since I've moved to using intents
, each Activity/Class has its own context, thus the previous dialogs and popup boxes from the Gui
class are in the background and not running.由于我已经开始使用
intents
,因此每个 Activity/Class 都有自己的上下文,因此Gui
class 中的先前对话框和弹出框位于后台且未运行。 I get an error saying android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@406629a0 is not valid; is your activity running?
我收到一条错误消息,
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@406629a0 is not valid; is your activity running?
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@406629a0 is not valid; is your activity running?
when I click on a button to launch a dialog.当我单击按钮启动对话框时。
To me, this indicates the new Intents have taken over the foreground and the objects from the previous context are out of scope.对我来说,这表明新的 Intent 已经占据了前景,并且来自先前上下文的对象不在 scope 之外。
So, is there a way I can still pass the same context through to the new Intents so I can still access these shared dialogs?那么,有没有办法我仍然可以将相同的上下文传递给新的 Intent,以便我仍然可以访问这些共享对话框? Or will I have to bring the code into each class (duplicate code)?
还是我必须将代码带入每个 class(重复代码)?
In case thats a bit hard to understand, here is some basic source code:如果这有点难以理解,这里是一些基本的源代码:
public class Driver extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Gui display = new Gui(this);
display.showScreen();
}
}
/////////////GUI.java///////////////////////
public class Gui
{
private Activity activity;
private Gui()
{}
public Gui(Activity _activity)//,Context _context)
{
this();
activity = _activity;
}
public void showScreen()
{
if(isLocationMode())
{
Intent i = new Intent(activity,LocationScreen.class);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(i);
//locatScreen = new LocationScreen(activity);
//mainLayout.addView(locatScreen.getView());
}
else if (isManageMode())
{
Intent i = new Intent(activity,ManageScreen.class);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(i);
//manageScreen = new ManageScreen(activity);
//mainLayout.addView(manageScreen.getView());
}
else if (isForwardMode())
{
Intent i = new Intent(activity,ForwardScreen.class);
//i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(i);
//forwardScreen = new ForwardScreen(activity);
//mainLayout.addView(forwardScreen.getView());
}
}
}
Have a setContext(Activity _activity) method in your Gui and call this in the onCreate of each activity?在你的 Gui 中有一个 setContext(Activity _activity) 方法并在每个活动的 onCreate 中调用它?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.