0
votes

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.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());
        }
    }
}
1
You have three separate screens, but one class that handles all of your UI? That's not really kosher in my mind, as you're running into problems with doing GUI work off the UI-Thread. Have you tried having your activities/screens extend a common "MyApplicationActivity" that could do the GUI work you don't want to duplicate across multiple activities? - jlindenbaum
If you need the dialog raised in first activity to be displayed in second w/o showing it again than you don't need separates activity at all and should keep it single activity. - ernazm
@jlindenbaum If I extend a common activity, and not make each screen extend Activity then I wont have an onCreate() method so I won't be able to call startActivity. - eoinzy
@ernazm I have one dialog that I use for 2 screens but the text on it changes depending on what screen I'm on. I didn't want to create a new dialog with buttons and clickListeners just to cater for different text, hence the shared dialog. - eoinzy
@eoinzy - no your common "MyApplicationActivity" you extend would extend "Activity" this way you still override onCreate etc., but you get to reuse common code, say, "showGenericErrorMessage()" to throw up a dialog, or something. - jlindenbaum

1 Answers

1
votes

Have a setContext(Activity _activity) method in your Gui and call this in the onCreate of each activity?