Comments (4)
@yudhir Let me add another example with Fragments. You can also look at this library https://github.com/Fueled/flowr
from android-starter.
ok. please add.
from android-starter.
I fixed and Implemented . All great! I hope I am not leaking memory.
@PerFragment
@Subcomponent(modules = FragmentModule.class)
public interface FragmentComponent {
void inject(BaseFragment baseFragment);
void inject(CartFragment cartFragment);
}
/**
* Abstract Fragment that every other Fragment in this application must implement. It handles
* creation of Dagger components and makes sure that instances of ConfigPersistentComponent are kept
* across configuration changes.
*/
public abstract class BaseFragment extends Fragment {
private static final String KEY_FRAGMENT_ID = "KEY_FRAGMENT_ID";
private static final LongSparseArray<ConfigPersistentComponent> sComponentsArray =
new LongSparseArray<>();
private static final AtomicLong NEXT_ID = new AtomicLong(0);
private FragmentComponent mFragmentComponent;
private long mFragmentId;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the FragmentComponent and reuses cached ConfigPersistentComponent if this is
// being called after a configuration change.
mFragmentId = savedInstanceState != null ?
savedInstanceState.getLong(KEY_FRAGMENT_ID) : NEXT_ID.getAndIncrement();
ConfigPersistentComponent configPersistentComponent;
if (sComponentsArray.get(mFragmentId) == null) {
Timber.i("Creating new ConfigPersistentComponent id=%d", mFragmentId);
configPersistentComponent = DaggerConfigPersistentComponent.builder()
.applicationComponent(MvpStarterApplication.get(
getActivity()).getComponent())
.build();
sComponentsArray.put(mFragmentId, configPersistentComponent);
} else {
Timber.i("Reusing ConfigPersistentComponent id=%d", mFragmentId);
configPersistentComponent = sComponentsArray.get(mFragmentId);
}
mFragmentComponent = configPersistentComponent.fragmentComponent(new FragmentModule(this));
mFragmentComponent.inject(this);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(getLayout(), container, false);
ButterKnife.bind(this, view);
return view;
}
public abstract int getLayout();
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putLong(KEY_FRAGMENT_ID, mFragmentId);
}
@Override
public void onDestroy() {
if (!getActivity().isChangingConfigurations()) {
Timber.i("Clearing ConfigPersistentComponent id=%d", mFragmentId);
sComponentsArray.remove(mFragmentId);
}
super.onDestroy();
}
public FragmentComponent getFragmentComponent() {
return mFragmentComponent;
}
}
public interface FragmentCallback {
void onFragmentAttached();
void onFragmentDetached(String tag);
}
public class CartFragment extends BaseFragment implements CartMVPview, ErrorView.ErrorListener {
...
//Fragment
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentComponent().inject(this);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mCartPresenter.attachView(this);
mErrorView.setErrorListener(this);
mCartPresenter.getAllCart();
}
}
//
Adding Fragment
public class ShopListActivity extends BaseActivity implements ShopListMvpView, ,ErrorView.ErrorListener,FragmentCallback {
...
@Override
public void openCart() {
getSupportFragmentManager()
.beginTransaction()
.disallowAddToBackStack()
.setCustomAnimations(R.anim.slide_left, R.anim.slide_right)
.add(R.id.cart_root, CartFragment.newInstance(), CartFragment.TAG)
.commit();
}
Removing
@Override
public void onBackPressed() {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(CartFragment.TAG);
if (fragment == null) {
super.onBackPressed();
} else {
onFragmentDetached(CartFragment.TAG);
}
}
@Override
public void onFragmentAttached() {
}
@Override
public void onFragmentDetached(String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(tag);
if (fragment != null) {
fragmentManager
.beginTransaction()
.disallowAddToBackStack()
.setCustomAnimations(R.anim.slide_left, R.anim.slide_right)
.remove(fragment)
.commitNow();
}
from android-starter.
@yudhir Good work :) You can open a PR for the same.
from android-starter.
Related Issues (20)
- Why is there ServiceFactory when there is also Dagger2? HOT 1
- No proguard rules HOT 3
- Lightweight app/build.gradle HOT 3
- Preview of view elements does not show in android studio HOT 1
- Broken imports in yo android-mvp-starter bundle HOT 4
- Could not read script '/path/to/project/app/dependencies.gradle' as it does not exist. HOT 2
- Migrate to dagger 2.10 please HOT 3
- Add chuck library for seeing network calls right on the phone. HOT 1
- Errors when opening the generated project in Android Studio 2.3.3 HOT 2
- Unable to inject SharedPreferences. HOT 3
- recreate fragment view error when onbackpress
- Configuration change issue
- Execution failed for task ':app:checkstyle'.
- SharedPreferences Issue
- Contribution guide
- Cannot resolve symbol DaggerTestComponent
- Base Fragment usage
- Firebase
- Using all these components into a full application
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from android-starter.