Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 5    Views: 96

Paul Rogers
Android Fan
Profile
Posts: 2524
Reg: May 30, 2011
UK
35,740
06/04/13 04:25 PM (12 years ago)

Some Android help needed please.

In a non-bt project, if I wanted to release the current activity from the stack and go back to a specific activity, I'd add something like this, usually to a button click: Intent i = new Intent(this, my_new_activity.class); i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(i); this.finish(); But because in BT a single activity might be used multiple times in an app for different things, how would I do the equivalent in bt without cloning the same plugin numerous times so they're all unique activities?
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
06/05/13 10:28 PM (12 years ago)
I'm not totally sure what you're asking here. I think I understand. Generally speaking you do exactly what you show here to "go back" in Android. Start a new activity and kill the existing activity. this.finish() does this. "this" refers to the Activity you're looking at on the screen, the one on top of the stack. In BT it's no different. There is an Activity on the top of the stack and you can kill it just like your example. This "because in BT a single activity might be used multiple times in an app for different things" has me guessing a bit. Yes, an Activity Class (a plugin in our world) may be used multiple times for multiple things but killing it and removing it from the stack doesn't change this. a) You load an Activity. Say for example a BT_screen_quiz. This Activity is on the top of the stack. b) You kill it with finish(). It goes away. The previous screen shows. c) You show another BT_screen_quiz. A new instance of the BT_screen_quiz Activity is created and placed on top of the stack. Nothing is different than your first explanation and the "normal" way you did it before. It's the same thing. Maybe you're not 100% on the idea of creating multiple instances of classes? The BT_screen_quiz is ONE class, it's parent class is Android Activity. So, in java, we describe this class. Next, to show a "screen" we create an instance of this class (now called an object, objects are instances of classes) then show it. We do this over and over again with each new screen. Android manages memory and kills things as they are finished() Hope this helps
 
Paul Rogers
Android Fan
Profile
Posts: 2524
Reg: May 30, 2011
UK
35,740
like
06/06/13 05:47 AM (12 years ago)
Thanks David, it does help. I'm struggling a bit with some concepts. I think this makes things a bit clearer, thanks!
 
ATRAIN53
Code is Art
Profile
Posts: 1755
Reg: Nov 17, 2011
Chicago
26,450
like
06/06/13 10:23 AM (12 years ago)
I think I know where he's coming from. I got caught by this myself and spent a few days before I gave up. I think this works with "single" activity based screen like a 'splash screen' or the "video player" or the "quiz" - But not with screens that have child items, like menus....or if you have multiple instances of the plug-in in your app. Lets say I have an activity that asks a user to 'login'. Once you login to the screen I wanted it to launch the home "menu" activity using that activity launch code: (lets say my home menu was created using Sue's awesome menu plugin) If I use this: Intent i = new Intent(this, wb_menuimage.class); startActivity(i); It will go to that wb_menuimage activity - but it is a blank menu and LOGCAT tells me 'NO Child items'. I know the menu works and has them - because that's how they got to the login screen! I tried pointing to simple menu or any of the other menu plugins we have. They all come up blank and complain about a lack of child items. Seems like some parsing is not occuring when you call the activty directly like that. I can point to another screen, but what if I have 7 of the same screen in my app like the video? Which instance of the plugin does it launch? I see this same "launch activity" code used in every android sample I look at, but no one uses activities the same way BT does.....
 
Paul Rogers
Android Fan
Profile
Posts: 2524
Reg: May 30, 2011
UK
35,740
like
06/06/13 07:14 PM (12 years ago)
Ok, here's a specific example. So the user is doing a quiz on 'level 2'. Three attempts and it's game over if they don't hit a pre-defined score. Lives are in the 'Try Again', btnAnswer_1, so if the btnAnswer_1 counter is less than 1 the activity is finished. Now if I just call this.finish(); on the quiz activity the activity to come to the top of the stack is the 'start of level 2' screen, but I don't want that, I want the accumulated score to reset to zero and the apps home activity to come to the top, so I fire an intent to "FLAG_ACTIVITY_REORDER_TO_FRONT" for that specific activity. Because I can't see a way to do this with bt, as ATRAIN53 has found, I create a copy of a menu plugin (in this case, BT_screen_menuButtons_start) and use it as a 'one off' activity, so then I can bring that 'one off' activity to the top: if(clickedButton.getId() == R.id.btnAnswer_1){ BT_item nextScreenObject = null; counter--; stringVal = Integer.toString(counter); value.setText(stringVal); if (counter >= 1){ startCountdown(); }else{ if (counter < 1) counter = 3; value.setText("0"); LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_toast_layout, (ViewGroup) findViewById(R.id.custom_linear_toast)); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); PrefsModel.saveAccumulatedScore(this,(long) 0); Intent i = new Intent(this, BT_screen_menuButtons_start.class); i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); startActivity(i); this.finish(); } }
 
David @ buzztouch
buzztouch Evangelist
Profile
Posts: 6866
Reg: Jan 01, 2010
Monterey, CA
78,840
like
06/06/13 10:50 PM (12 years ago)
From... "It will go to that wb_menuimage activity - but it is a blank menu and LOGCAT tells me 'NO Child items" If you do this (Atrain's example)... Intent i = new Intent(this, wb_menuimage.class); startActivity(i); Indeed you're creating a new instance of the wb_menuimage class. It makes sense that no data or child items or anything else would be in that instance because it has no idea what it's data is. The class is instantiated (it's an activity) and it displays but it has no data. If you were to use the loadScreenObject() method in the BT_act_controller class it would be different. Have a look at that method, it takes 3 arguments. a) The activity you're currently looking at. The "parent" activity. b) The screen data, the JSON, for the screen you're currently looking at. The "parentScreenData" c) The data, the JSON, for the screen you want to load. This JSON will have an itemType:wb_menu_image along with all the childItems and other data for the screen. This methods definition looks like: public static void loadScreenObject(Activity parentActivity, BT_item parentScreenData, BT_item theMenuItemData, BT_item theScreenData); In summary: Creating an instance of a class who's parent class is an Android Activity (like all the plugins) then loading it with startActivity will start the activity but it does not know what it's data is. As far as killing Activities (finishing) it's the same in BT as in any other Android app.
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.