Thursday, April 5, 2012

Preventing another instance of an activity from launching (menu bar)

Had a feature request on an Android app for a customer where they didn't want to launch a new version of an activity from the menu IF the activity was currently the top of the activity stack (currently visible screen). Easy enough by adding a flag to the intent. There are some other ways you can do this as well by modifying the manifest. Here's an interesting article on activity launch mode that explains in gory detail the different types. The folks out here have a great article on Android activity launchmode. Be sure to check it out! http://intridea.com/posts/android-understanding-activity-launchmode
 switch (item.getItemId())  
           {  
           case GlobalConstants.MENU_ID_ITEM1:  
                Intent home = new Intent(this, Plan.class);  
                home.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
                startActivity(home);  
                break;  
           case GlobalConstants.MENU_ID_ITEM2:  
                Intent intent = new Intent(this, Practice.class);  
                intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
                startActivity(intent);  
                break;  
           case GlobalConstants.MENU_ID_ITEM3:  
                Intent intent2 = new Intent(this, States.class);  
                intent2.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
                startActivity(intent2);  
                break;  
           case GlobalConstants.MENU_ID_ITEM4:  
                Intent intent3 = new Intent(this,Connect.class);  
                intent3.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
                startActivity(intent3);  
                break;  
           case GlobalConstants.MENU_ID_ITEM5:  
                Intent intent4 = new Intent(this,Games.class);  
                intent4.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
                startActivity(intent4);  
                break;  
           default:  
                break;  
           }