开发者

Share activity context among intents

I have 3 screens in my app, each of which are in their own classes. When the app launches, my Driver class sets up some GUI elements, and then launches the first Intent.

I have a separate GUI class (which Driver invokes) which handles everything from menu's to dialog boxes. 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.

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. 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? 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.

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? Or will I have to bring the code into each class (duplicate code)?

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.jav开发者_StackOverflowa///////////////////////
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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜