android - Fragments and Activities -- where do I put my application logic? -
i have created view how want look. has 1 images, input box, , button. want load activity when button clicked. confused why there fragments , activities. new android world (coming ios).
my understanding activities similar viewcontrollers, not sure understand fragment is.
where put event handling?
package com.phppointofsale.phppointofsale; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.os.bundle; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.os.build; public class storeurlactivity extends actionbaractivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_store_url); if (savedinstancestate == null) { getsupportfragmentmanager().begintransaction() .add(r.id.container, new storeurlfragement()).commit(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.store_url, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * placeholder fragment containing simple view. */ public static class storeurlfragement extends fragment { public storeurlfragement() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_store_url, container, false); return rootview; } } }
firstly recommend reading fragments . pay particular attention created fragment section, includes fragment life-cycle diagram. second download , compile sample app,the effective navigation app understand how different fragments work in tandem, , implements action bar.
to answer question more or less fragment can thought of separate class. once call upon particular fragment can call functions within class.
fragment case-switch
this sample code show mean.
public fragment getitem(int i){ switch (i) { case 0: // first section of app interesting -- offers // launchpad other demonstrations in example application. return new launchpadsectionfragment(); case 1: return new bluetoothclass(); default: // gps section of app . fragment fragment = new dummysectionfragment(); bundle args = new bundle(); args.putint(dummysectionfragment.arg_section_number, + 1); fragment.setarguments(args); return fragment; } }
in case each fragment me represented class, implemented in separate tab , each tab had separate functionality. 1 of key advantages of fragments can run separate activities without first letting 1 activity complete.
furthermore each fragment extension of java.lang.object library. has functions + additional ones. read this well. lastly idea have separate xml files each fragment can display separately when fragment invoked.
some more code
each fragment will/could have this
public void onactivitycreated(bundle savedinstancestate){ super.onactivitycreated(savedinstancestate); // stuff on creation. add bulk of code. clicklistners view rootview = inflater.inflate(r.layout.xml_the_fragment_uses container,false); rootview.findviewbyid(r.id.your_id).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { //do } }); } public void onstart(){ super.onstart(); toast.maketext(getactivity(), "fragment started",toast.length_short).show(); } public void onresume(){ super.onstart(); toast.maketext(getactivity(), "fragment resumed",toast.length_short).show(); } public void onstop(){ super.onstart(); toast.maketext(getactivity(), "fragment stoped",toast.length_short).show(); disablebt(); }
remember these functions fragment life-cycle mentioned earlier.
hopefully gave idea on fragments. remember read this lot of functionality uses v7 app compat library. including fragment manager.
Comments
Post a Comment