Android free/paid - architecture using library - android

I need to create a free and paid version of the app and therefore I am using a library project with the common activities and resources in. However I was wondering how you would go about organising this:
A shared (library) activity MyActivity.java launches a DialogFragment when a user clicks a buttons. The DialogFragment will be different for the free and paid apps. For the free app we will launch FreeDialog.java and for paid we will launch PaidDialog.java. These two dialog fragments will not be in the library project as they are not shared; they will be in the free and paid projects separately.
The only thing I can think is to make MyActivity.java abstract and to extend it in the free and paid projects. The extensions will launch their respective DialogFragments.
Another way could be to overload the Fragments in each of the projects. However, I am not sure if this is possible.

Theoretically, you could make MyActivity abstract, and subclass it in both paid and free app projects. But, Activity classes are a little different than normal Java classes, which complicates this. They are listed in AndroidManifest.xml, and then also are not created by simply newing them up. They're normally created with Intents. So, I think I would stay away from making the Activity an abstract class.
I think what you really want is to use is something like the Factory pattern to create an instance of DialogFragment, where your common library does not know which DialogFragment will be created. You can get pretty fancy with a factory, and you can read all about that elsewhere, but a simple one might work for you:
public class FragmentFactory {
private static String _dialogFragmentClassName;
public static void registerFragment(String className) {
// you might choose to assert if _dialogFragmentClassName != null
_dialogFragmentClassName = className;
}
public static DialogFragment createFragment() {
try {
return (DialogFragment) Class.forName(_dialogFragmentClassName).newInstance();
} catch (Exception e) {
return null;
}
}
}
Then, in your free and paid app project code, you would issue calls like this at startup (e.g. in a main Activity):
FragmentFactory.registerFragment(com.mycompany.free.FreeDialogFragment.class.getName());
and
FragmentFactory.registerFragment(com.mycompany.paid.PaidDialogFragment.class.getName());
Finally, in the common library code, you can create an instance of the fragment by calling
DialogFragment fragment = FragmentFactory.createFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, fragment);
ft.commit();

Related

How to use android.support.v4.app.Fragment with Activity class?

Ok here is the issue, the native Android Fragment is deprecated.
This will be use in an Unity Android native plugin.
In the old ways you simply create a Fragment class like this:
public class UnityAndroidNativeplugin extends Fragment
{
public static void Init()
{
instance = new UnityAndroidNativePlugin
UnityPlayer.currentActivity.
getFragmentManager().
beginTransaction().add(instance, UnityAndroidNativeplugin.LOG_TAG).commit();
}
}
That works fine but I still don't get, if this is deprecated we should use the support fragment library. Ok I will update this.
So when I change to the new version it should be something like this.
public class UnityAndroidNativeplugin extends android.support.v4.app.Fragment
{
public static void Init()
{
instance = new UnityAndroidNativePlugin
FragmentManager fragMan = UnityPlayer.currentActivity.getSupportFragmentManager();
//This Fails
}
}
I already know that my main Activity should be a FragmentActivity.
Cannot call getSupportFragmentManager() from activity Check here.
So it's not possible to use android.support.v4.app.FragmentManager while using Android Activity (android.app.Activity)?
One solution is to override the Unity Main activity but that could have problems when using other plugins. Or maybe using something else than a fragment.
So it's not possible to use android.support.v4.app.FragmentManager while using Android Activity (android.app.Activity)
Correct.
if this is deprecated we should use the support fragment library
Or, better yet, the AndroidX one, as the Support Library one will become obsolete before long.

Generic purpose fragment activity

I do not want to create new Activity for each task as the business logic is handled by custom Fragment already. There is a generic layout:
<FrameLayout .. >
<LinearLayout ..
android:id="#+id/fragment" />
</FrameLayout>
I wanted to create generic Activity that would use it and be initialized by a specific Fragment.
public class LeliFragmentActivity extends LeliBaseActivity {
public static final String KEY_FRAGMENT = BuildConfig.APPLICATION_ID + ".FRAGMENT";
protected void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_with_fragment);
Fragment fragment = (Fragment ) getIntent().getSerializableExtra(KEY_FRAGMENT);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment, calcFragment);
transaction.commit();
}
public static void start(Context c, LeliBaseFragment fragment) {
Intent intent = new Intent(c, LeliFragmentActivity.class);
intent.putExtra(KEY_FRAGMENT, fragment);
c.startActivity(intent);
}
But I feel that this not a good way. Passing Fragment in the Intent seems to me as overkill. All this serialization is costly. I could pass class name instead .. Or is there better way? Or shall I have one Activity for each (full screen) Fragment? Thanks for hint.
First
You can use one Activity for whole project if it's convenient for you and project is not too big. But if project is big and contains a lot of Fragments it's better to divide them into logical order on different activities. For example LoginActivity will represent all sing/registration flow with many Fragments. Than LeliFragmentActivity with all needed Fragments
Second
You don't have to pass your Fragments to each other. To communication between Fragment the best and simple way to use intrefaces like documentation says.
Other possible way to communication is event bus libraries like EventBus or Otto.
Try this:
https://gist.github.com/burnermanx/8e80e160dfba2f758546c037fce5269a
I made it based on your question, but it will not be useful for me for now.

Illegalstateexception activity has been destroyed fragmenttransaction.commit()

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.

Where to place application logic in new Activity code generated by ADT 22.6.2

In the latest versions of eclipse (ADT v22.6.2) the create android application now generates an activity_main.xml and a fragment_main.xml. It generates only a single activity class
but this now has an embedded inner static fragment class that is created by the activity in its onCreate method
#Override
protected void onCreate(Bundle savedInstanceState) {
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
....
public static class PlaceholderFragment extends Fragment
My confusion is how to port old code/examples where there was only 1 activity and the main application
logic is usually put in the Activity onCreate method i.e. stuff like findViewById. Listeners
etc
The way I have approached it is that I put all my user created views as members of the static PlaceHolderFragment class. Then I call this in the fragment onCreateView. I still have some logic in the activity and it stores a pointer to the fragment. It updates the views members by calling getters on the fragment. Is this correct or should all logic be moved to the fragment now ?
All the tutorials/examples use the old approach where logic is placed in the activity
so there is no reference documentation for how to use the new files that Eclipse generates for an Android application. Any help appreciated ?
Don't worry about the files that Eclipse generate automatically: You can do whatever that you want!!!
Fragment is a element between an Activity and a container.That's mean that you can put the logic of your code inside of one fragment with not problems.
In theory, fragments are used when you want to manage screens using different modules, containers. It's a fragment, a module, part of one screen (but also can be used in a full screen looking as an activity and with the same behaviour than one activity.) For example, imagine that you have for mobile phone screens, one list of news in one screen, and when you click, your app go to the next screen for show the content of the news, right? Ok, so you can use for these 2 screens: 2 activities for each one or one parent activity with 2 fragments...whatever that you want...
Imagine the same case, for a tablet version of your app, now the left part of the screen you should show the list of news and in the right part of the screen, the contain of each news clicked, right? In that case, would be completly necessary to use one activity parent with two fragments...in that case, we could reuse almost the same case for the mobile phones or tablet.
And now, focus in your question: if you don't want complicate the life (not too much, because work with fragment is easy too) I will recomend you to use only activities for your app.
But, like your question, you want to port, there isn't any problem. Now imagine that your activity is going to be only the class where you are going to manage the fragments. The logic for each screen has to be in each fragment. Use the activity only for replace fragments, or share information between fragments, etc. The activity will be like the orchestra director.
From each fragment, you can access to methods or public variables of your activity using ((NameOfActivity)getActivity()).
Is it clear for you?
One more stuff, in fragment, normally the method that we used for initialize stuffs is onCreateView (and not onCreate like activities).

Android UINavigationController-like feature

On the iPhone I use a Navigation Controller to push and pop Views from. Very handy.
Is there an equivalent in Android?
This is an old question, but I believe the answer has changed. It is now possible to imitate the Nav stack in iOS in android using Fragments.
http://developer.android.com/reference/android/app/Fragment.html
Basically instead of jumping from Activity to Activity you instead stay in one Activity that controls the display, organization, and animation of Fragments which each contain their own behavior much like the NavController / UIViewController model in iOS.
It is also backwards compatible as a static library so you can implement it on pre-Honeycomb devices.
Strategies for Honeycomb & backward compatibility
Typically in android, each view is displayed in its own Activity. You can read about activities in the application fundamentals documentation. To move to a new Activity, or view, you use an intent.
If you haven't done so yet, I'd highly recommend reading through those introductary android docs. They aren't too long, and do a good job of explaning the basic program structure.
I made a Framework (github) to provide a hierarchical navigation pattern, with animations to provide sense of navigation, rather than launching new Activities every time.
Here's how to use it:
Add the framework to your project as a Module
Add a new Java class in your project ("File - New - Java Class").
Note: If you are editing the Activity.java file that provides you the template, delete all its implementations and leave it empty.
Make it extend NavigationActivity
Implement all the NavigationActivity abstract methods
(in Android Studio if you click Alt + insert and select implement - methods all the function definitions are automatically generated).
public class NavigationTest extends NavigationActivity{
#Override
public Fragment firstFragment() {
//return the first fragment that will be shown
}
#Override
public Boolean showBackButtonInFirstFragment() {
//show back button already in the first Fragment
//set to True if this activity is called by another Activity
//the back button will then pop back to the previous Activity
}
#Override
public Boolean showMasterDetailLayoutInTablets() {
//set to false if you don't want a master-detail layout in tablets
}
}
Presenting a new Fragment
You can present a new fragment (with a nice animation) by calling the pushFragment method from NavigationActivity.
public void pushFragment(Fragment newFragment, animationType animation, boolean showAsDetailFragmentIfPossible)
newFragment (Fragment): New Fragment that will be presented
animation (animationType): Animation type enum: RIGHT_TO_LEFT, BOTTOM_TO_TOP, FLIP
showAsDetailFragmentIfPossible (boolean): If set as True, the user is in a Tablet, and you are using a master-detail layout, the Fragment will be shown in the detail Fragment (the panel in the right)!
Since you can access the activity from any Fragment with the getActivity() method, you can show a new Fragment from the currently displaying Fragment.
For example you can put this code within a button click listener:
NextFragment f = new NextFragment();
NavigationActivity nav =((NavigationActivity)getActivity());
nav.pushFragment(f,NavigationActivity.animationType.RIGHT_TO_LEFT,false);
You don't have to worry about implementing the back button behaviour. This is handled automatically by the NavigationActivity class.
There are thee basic types in Android to show UI in Android:
View
Fragment
Activity
Google IO 2018 introduced Navigation component which should make life easier. It is a wrapper under a standard mechanisms.
Here you can find NavGraph which looks like storyboard and NavController which help to navigate to destination

Categories

Resources