Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setSupportActionBar in a view that extends LifecycleActivity

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?

like image 510
alexpfx Avatar asked Jun 20 '17 23:06

alexpfx


2 Answers

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;
    }
}
like image 117
CommonsWare Avatar answered Nov 16 '22 00:11

CommonsWare


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.

like image 30
catsfw Avatar answered Nov 16 '22 01:11

catsfw