android - lauching different activities depending on a value -
i'm trying start new activity depending of value of parameter when user clicks next button
<button android:id="@+id/next" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/next" /> i think shouldn't use code know launch new activity, because launchs single activity.
android:onclick="lanzaractividad" my purpose user sets parameter (a number) , each number corresponds different activities.
i guess should write similar (in oncreate method)
button btn=(button)findviewbyid(r.id.next); int parameter=1; //it contains number, in our case, might instance 1 btn.setonclicklistener(new view.onclicklistener() { public void onclick(view v) { switch(parameter) { //now problem. case 0 : lanzarcalculate(v); case 1 : lanzarcalculate2(v); } } }); the methods creates intents are
public void lanzarcalculate(view view) //si apretamos el boton calculate { intent = new intent(this, calculate.class); bundle b = new bundle(); b.putdouble("time", tau); i.putextras(b); startactivity(i); } public void lanzarcalculate2(view view) { intent = new intent(this, calculate2.class); bundle b = new bundle(); b.putdouble("time", tau); i.putextras(b); startactivity(i); } enter code here
thank much, hope can me.
i think problem unsure of how use intent "dynamically". how have done keep repeating code
@override public boolean onmenuitemclick(menuitem item) { intent intent = new intent(); // first, create intent object string nextact = null; // name activity start intent string package = "com.my.package."; // set package name int flag = -1; // in case want set flags depending on activity switch (item.getitemid()) { case (r.id.logout): // mine menu options use ints have nextact = package + "loginscreen"; flag = intent.flag_activity_clear_top; break; case (r.id.changelocation): nextact = package+ "changelocation"; // package reused in cases different me break; default: toast.maketext(getapplicationcontext(), "item not available", toast.length_short).show(); } try { if (nextact != null) { intent = new intent(baseactivity.this, class.forname(nextact)); // change string setting activity name if (flag != -1) { intent.setflags(flag); } // set flags if had startactivity(intent); // start activity } this popupmenu can same thing in onclick(). have added comments in code explain doing.
also, this
android:onclick="lanzaractividad" would fine in xml. this, wouldn't need define button , listener
public void lanzaractividad(view v) { // code above } this function called , run when click button
edit
it seems underlying problems need use youractivityname.this instead of this when create intents. also, add break; statements in each case or go on , run next case. above code keep needing rewrite intent code since of same
Comments
Post a Comment