I've searched hours for examples of multiple intents and Fragments within ActionBarActivity. I am learning android programming from Deitel series and other well informative resources. The obstacles which I'm facing are to start with a simple "Hello World" program from two intents using Fragments, but the resources I'm learning from doesn't contain examples extending ActionBarActivity but they all extend Activity.
The roadblock for me is inflating fragments from ActionBarActivity using its available methods. If i can acheive this simple task, my learning curve would excel immenesly.
ActionBarActivity extends FragmentActivity, which extends Activity. So, everything that Activity does, ActionBarActivity does it too.
ActionBarActivity provides access and control for the ActionBar, while FragmentActivity provides a FragmentManager, that is used to manage your Fragments.
ActionBarActivity provides you all these features.
There are lots of examples on the web and this site is the right place to start.
Here is what I think you don't understand.
1. ActionBarActivity vs Activity
Don't worry about ActionBarActivity vs Activity. ActionBarActivity extends Activity so all the code that is valid in Activity is present in AcctionBarActivity. Please check this link.
2. Inflation
Usually what I do is make a separate class for each of my fragments as such,
public class Fragment1 extends Fragment{
// in the onCreateView I create a rootView & inflate.
final View rootview = inflater.inflate(R.layout.your_layout_file, container,false);
// Now you can use rootView to call all your Activity functions. Such as findView
//onClick etc.
return rootview;
3. In the MainActivity
This purely depends on the way you want to structure your app. But certainly you must have a getCount and getItem function. Like so.
#Override
public Fragment getItem(int i){
switch (i) {
case 0:
return new Fragment1();
case 2:
return new Fragment2();
}
}
#Override
public int getCount() {
return 2;
}
Related
I have a few fragments in PageAdapter which placed on other fragment and I need somehow to control their states, update some views, etc.
I have code which looks like below:
Fragment frg1;
Fragment frg2;
frg1.setHandleProtocol(this);
class MyAdapter extends FragmentPageAdapter {
public Fragment getItem(int pos) {
return frg1;
}
}
This is very simple snippet, but it should help me to explain. This approach works good by the moment when Activity will be killed and restored by Android. In that case PageAdapter re-creates new fragments by Object class and they won't have a old links and depends.
Is there any good way to handle this new fragments?
I have ugly workaround. I am just going through all fragments in FragmentManager and re-assigning their to the old links.
I am more familiar with iOS development than Android and I am wondering if all code should be written in an Activity rather than having a "model" class.
I have a couple screens each with a few checkboxes and I want them all to behave the same on click, I am trying to figure out how I would do this without writing repeating code in each activity. Thanks!
No you should not. If you are familiar with java, think of an activity as a extension of main with OO added.
In your particular example you can create a class with a method like:
<MethodName>(View <checkboxClickedName>){ //your code here }. and then add this to the checkbox in the XML android:onClick="<MethodName>", you may need the full package path (e.g. com.example.app.)
Note: if some of the commands/objects you need are only available within an activity you should create this in an calss that extends Activity or preferably within the running activity.
You could have a base class that extends activity that contains the methods that you want executed on click (either implemented or abstract). Use this new base class instead of activity when making new activities. In the layout xml, you can set the onclick of each checkbox to be the method in the base activity you want executed.
The best practice would be to use a single activity and switch fragments as if they were your screens. Then, the activity could simply implement the listener interface that the fragments would re-use.
Since you have multiple activities this becomes a little bit harder. To really re-use a single listener, I can think of a single (not so beautiful) option. Create a static listener and lazy load it:
public class MainActivity extends Activity {
private static View.OnClickListener sCheckboxClickListener;
public static View.OnClickListener getCheckboxClickListener() {
if (sCheckboxClickListener == null) {
sCheckboxClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// Work with v
}
};
}
return sCheckboxClickListener;
}
}
And in each of your activities call:
findViewById(R.id.checkbox1)
.setOnClickListener(MainActivity.getCheckboxClickListener());
I have seen a few versions of this question before, but the reasons for this exception were different than my own it seems.
What I am trying to do:
-Main Activity class has a toolbar at the bottom, clicking the buttons will display a series of fragments, one after another.
- A class EditItemFragmentManager, which is instatiated on a button click, and has methods that display specific fragments based on the toolbar button clicked.
I would like to use this manager class I created because it cleans my code up significantly and will make adding more features later helpful.
Here is my EditItemFragmentManager class, I am not sure if extending Activity is a good idea or not, I think that it will put my MainActivity on pause
public class EditItemFragmentManager extends Activity{
//instance variables
public EditItemFragmentManager(){
// initialization of some variables
}
public void editItem(){
editItemSequence();
}
private void editItemSequence(){
EditNameFragment enf = new EditNameFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(editNameFragment, EDIT_FRAG_TAG);
fragmentTransaction.addToBackStack(EDIT_FRAG_TAG);
fragmentTransaction.commit();
}
}
So it blows up when commit(); is called, giving me
java.lang.IllegalStateException: Activity has been destroyed
This is how I am trying to get this fragment from my MainActivity,
#Override
public void onClick(View view) {
EditIteFragmetManager manager = new EditIteFragmetManager();
manager.editItem();
}
I am still learning about the Acvtivity lifecycle in Android. I think my problem is something due to this class extending Activity, which puts my Main on pause, and the FragmentTransaction has nothing to commit to? If so, I need to get the existing instance of my main activity and call it on that? This is where I'm a bit lost, if anyone who understands the lifecycle of Activities/Fragments explain how I could go about implementing this while still having a helper class such as this?
If you're using the SupportFragmentManager, then you need to extend from FragmentActivity, and not just Activity. Also make sure that you imported the Fragment from the v4 support library, and not android.app.
Other than that, you seem to be instantiating a subclass of Activity with "new", which is terrible. Create activities only using Intents.
I solved this issue by moving my manager class to become a private inner class of my main, since they are so tightly coupled. No fragment issues now.
I came upon this solution to use a Listfragment within a Tab of the Actionbar
I face the same problem as described there, but somehow this solution doesn't work for me.
I want to write an application for android versions greater than 4.0.
I took the Android Sample code for generating Swipe View with tabs, if you generate a new Project and select: Fixed Tabs+Swipe
one Fragment should be a ListFragment, so I came upon this solution but the problem is the MainActivity uses the: import android.support.v4 support.
Here is a part of my SectionsPagerAdapter the rest of the code is equal to the prelinked thread.
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ControlFragment();
case 1:
fragment = (Fragment)new WrapperListFragment();
}
return fragment;
}
I guess the code here useses the newer packages, so how to combine these two.
In general i want to use the actionbar with swipe feature, and one fragment should display a listView.
Thanks in advance.
In my application there are 14 activities. Out of that 9 activity contains custom title bar and tab pane. so here I need to write this common code at one place instead of redundant code in each activity that contain custom title bar and tab pane code (i.e layout and it's activity specific code)
What are the possible ways to do this?
The common way is:
Create a super class called, for instance, CommonActivity which extends Activity
Put the boilerplate code inside that class
Then make your activities extend CommonActivity instead of Activity:
Here a simple example:
public class CommonActivity extends Activity{
public void onCreate(Bundle b){
super.onCreate(b);
// code that is repeated
}
protected void moreRepeatitiveCode(){
}
}
And your current activities:
public class AnActivity extends CommonActivity{
public void onCreate(Bundle b){
super.onCreate(b);
// specific code
}
}
Hmm.. Common code doesn't always need to be in Activity class but just regular class. Than we could call those methods according to our needs referring to the common code class.
Am I right with this example?
Of course in case we need it like Activity, above proposal would work perfectly if we take care of Activity lifecycle and we don't forget to add it to manifest file.
In general Activities should just create UI, handle events occurrences and delegate business logic and/or other actions to the other components in our App.
Cheers