How to setSupportActionBar in a view that extends LifecycleActivity
W

2

17

I had an Activity that extended AppCompactActivity, and in onCreate method I setted the Toolbar using setSupportActionBar method in the usual way:

public class StepMasterActivity extends AppCompatActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_step_master);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);`
    }
}

But now I have a ViewModel component and to share data between fragments that are the children of this activity and manages lifecycles I have to get this component in Activity and so I make this extend LifecycleActivity.

public class StepMasterActivity extends LifecycleActivity {

    @Override
    public class StepMasterActivity extends LifecycleActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_step_master);
        // setToolbar();
        SharedViewModel sharedViewModel = ViewModelProviders.of(this).get(SharedViewModel.class);
    }
}

But I noticed that LifecycleActivity has nothing to do with AppCompatActivity neither FragmentActivity does.

public class LifecycleActivity extends FragmentActivity implements LifecycleRegistryOwner {
    private final LifecycleRegistry mRegistry = new LifecycleRegistry(this);

    public LifecycleActivity() {
    }

    public LifecycleRegistry getLifecycle() {
        return this.mRegistry;
    }
}

Am I doing something wrong?

Waynant answered 20/6, 2017 at 23:10 Comment(0)
D
38

UPDATE 2017-10-05: LifecycleActivity has been deprecated. If you use 26.1.0 or higher of support-fragment and appcompat-v7, both FragmentActivity and AppCompatActivity implement LifecycleOwner.

The original answer appears below for historical (and possibly hysterical) purposes.


Quoting the documentation:

Note: Since the Architecture Components are in alpha stage, Fragment and AppCompatActivity classes cannot implement it (because we cannot add a dependency from a stable component to an unstable API). Until Lifecycle is stable, LifecycleActivity and LifecycleFragment classes are provided for convenience. After the Lifecycles project is released, support library fragments and activities will implement the LifecycleOwner interface; LifecycleActivity and LifecycleFragment will be deprecated at that time.

LifecycleActivity is tied to FragmentActivity, not AppCompatActivity.

You should be able to create your own AppCompatLifecycleActivity as follows:

public class AppCompatLifecycleActivity extends AppCompatActivity implements LifecycleRegistryOwner {

    private final LifecycleRegistry mRegistry = new LifecycleRegistry(this);

    @Override
    public LifecycleRegistry getLifecycle() {
        return mRegistry;
    }
}
Dolores answered 20/6, 2017 at 23:20 Comment(6)
LifecycleRegistry is just that? so I could create custom "LifecycleRegistryOwner " instead of making fragments extend LifecycleFragment? (for now)Waynant
@alexpfx: Correct. Once again, quoting the documentation: "Any custom fragment or activity can be turned into a LifecycleOwner by implementing the built-in LifecycleRegistryOwner interface (instead of extending LifecycleFragment or LifecycleActivity)."Dolores
@superarts.org: I do not see what that has to do with this question, let alone this answer. Please ask a separate question, where you provide a minimal reproducible example and explain your problem in greater detail.Dolores
@CommonsWare, Wow. Works like charm.Bowse
@HirenPatel: If you upgrade to 26.1.0 or higher of the support libraries, you do not need this snippet. FragmentActivity implements LifecycleOwner now, and since AppCompatActivity extends FragmentActivity, AppCompatActivity also implements LifecycleOwner.Dolores
Fine. Thanks for sharing it.Bowse
H
3

The most recent support library revision 26.1.0 will allow you to use AppCompatActivity

Fragment and FragmentActivity (the base class for AppCompatActivity) now implement the LifecycleOwner interface from Architecture Components.

Halfandhalf answered 15/9, 2017 at 15:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.