android - What's the reason of activity self duplication after some action in handler? -
i'm developing game uses bluetooth connect other devices. it's standard client-server scheme application.
when player, whom device acting server, starts game, activity started. in oncreate method set , show progressdialog. dialog appearing until other player connects. in activity there handler receives messages background thread manages bluetooth connections. when first player joining game, handler receive proper message, , started initializing game, means dismiss progressdialog , load layout in setcontendview(view v). , there problem because works fine, new instance of activity created (while original 1 still exists) , takes on game management. , wouldn't major problem, when game ends, , new activity finished, original 1 gets on top , when try close it, app crashing.
i thought it's because of usage of setcontendview(view v) in wrong place, happened if player joining during game's term (then setcontendview(view v) not called again) in extreme case 3 instances of activity existing @ same time, causing mess , crash of players.
code sample:
public void oncreate(bundle savedinstancestate) { pdialog = new progressdialog(this); pdialog.setmessage("..."); pdialog.setcancelable(false); pdialog.setcanceledontouchoutside(false); pdialog.setbutton(dialoginterface.button_negative, context.getstring(r.string.alert_button_cancel), new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { pdialog.dismiss(); finishactivity(); } }); pdialog.show(); } and handler:
public void handlemessage(message message) { if(message.what == constants.message_new_player) { newplayermessage m = (newplayermessage) message.obj; if(pdialog != null && pdialog.isshowing()) { pdialog.dismiss(); } bluetoothservice.broadcast(new playersupdatemessage(game.getallplayersfields())); bluetoothservice.broadcast(game.addmessage(new chatmessage(m.getaddress(), chatmessage.player_join))); if (game.getdrawingplayeraddr().equals("")){ game.setdrawingplayeraddr(bluetoothservice.my_address); //... setgamemode(true); setgametimer(game.getroundtime()); } } //... } where setgamemode() contains mentioned setcontendview(view v) , initialization of buttons , other layout elements.
i have no idea what's reason of strange behaviour. seems it's totally random. noticed it's more probable on less powerfull devices.
to evade creating duplicate activities should set property launchmode
android:launchmode="singletask" here example:
<activity android:name=".mainactivity" android:label="@string/app_name" android:launchmode="singletask" /> this create activity @ root of new task , route intent it. however, if instance of activity exists, system routes intent existing instance through call onnewintent() method, rather creating new one
detailed information each of available options in:
http://developer.android.com/guide/topics/manifest/activity-element.html
Comments
Post a Comment