1

As I'm adding a cast button which only supports classes that extend the ActivityFragment class, I found it difficult since the specific class I'm interested in already extends another LinearLayout class. To avoid creating a custom button, I did the following workaround,

public class metaView extends LinearLayout {
    private FragmentActivity fragmentActivity;
    private void initViews() {
        fragmentActivity = new FragmentActivity(){
            @Override
            protected void onCreate(Bundle savedInstanceState){
                    //Create Button Discovery...

            }
        };
    }
}

However, it does not seem like the overriden method onCreate ever gets called. Does anyone see the problem with this alternative? or would this just not work?

Thanks in advance!

jensiepoo
  • 571
  • 3
  • 9
  • 26
  • 1
    I'm not familiar with the Android lifecycle, but it may have to do w/ the order of events that your methods are being called. For example, if fragmentActivity variable is being accessed before initViews() is called, then the object wouldn't have yet been created and as such, would not have the overriden method defined. Have you tried debugging this? – ryuu9187 Oct 16 '15 at 19:15
  • Thanks for the response! I should've made it clear, but initViews is accessed before fragmentActivity. But even if it wasn't, fragmentActivity wouldn't have possibly be accessed before initViews since it's contained within the method? – jensiepoo Oct 16 '15 at 19:25
  • 1
    Well, technically, I have no clue as to how fragmentActivity is being accessed, or what is calling onCreate(). But yes, I would expect a null reference ex if it were accessed before. What is supposed to be calling onCreate()? – ryuu9187 Oct 16 '15 at 19:28
  • 1
    If you are never using the FragmentActivity anywhere, then why are you creating one? I'm confused as to what you're actually trying to do. – NoChinDeluxe Oct 16 '15 at 19:29
  • onCreate is where you initialize an activity, which should be invoked as you instantiate FragmentActivity – jensiepoo Oct 16 '15 at 19:29
  • 1
    Check it out: http://stackoverflow.com/questions/7475356/is-oncreate-called-when-an-activity-object-is-created. onCreate is apparently not called when manually instantiating the object. – ryuu9187 Oct 16 '15 at 19:31
  • @drschultz I'm trying to add a chromecast discovery callback in onCreate. – jensiepoo Oct 16 '15 at 19:31
  • @jason9187 Wooh, that could be the problem.. Lemme look into it.. Thanks a lot.. – jensiepoo Oct 16 '15 at 19:32
  • 2
    Well what it sounds like is you actually need to display your content in a FragmentActivity, and use a LinearLayout within that to actually place the content. What you're doing in the code doesn't make a lot of sense. You wouldn't normally build an Activity from a Layout. Why can't you just set the callbacks in the Activity you're using to actually display your content? – NoChinDeluxe Oct 16 '15 at 19:35

1 Answers1

1

As I'm adding a cast button which only supports classes that extend the ActivityFragment class

That doesn't make any sense. Regardless, you cannot create and use an anonymous Activity instance. Your layout should be configurable to show/hide the button, and provide an interface to handle the click on the button. For example your MetaView class could have something like this:

public interface OnCastButtonClickedListener {
    void onCastButtonClicked();
}

public void setCastButtonEnabled(boolean enabled) {
    // Turn it on or off
}

public void setOnCastButtonClickedListener(OnCastButtonClickedListener l) {
    // Assign some listener to delegate the button click to.
}

And then, whatever Activity is using the layout becomes responsible for showing/hiding the cast button, and handling the click event.

Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • I'm looking at https://plus.google.com/+GoogleDevelopers/posts/ZHHe2tE7hcg Your solution definitely works, which seems like the third option(custom button) which I'm trying to avoid. – jensiepoo Oct 16 '15 at 19:41
  • 1
    Check this implementation -- I think this is what you want: https://github.com/googlecast/MediaRouter-Cast-Button-android/blob/master/src/com/example/mediarouter/MediaRouterActionBarButtonActivity.java – Kevin Coppock Oct 16 '15 at 19:43
  • Thanks for the help! I was actually looking at this https://github.com/googlecast/MediaRouter-Cast-Button-android/blob/master/src/com/example/mediarouter/MediaRouterButtonActivity.java But this still wouldn't solve the problem though? since my class already extends a LinearLayout class... – jensiepoo Oct 16 '15 at 19:47
  • 1
    @jensiepoo That should work too if you need to put the cast button somewhere else -- you just need to provide an accessor for the button on your custom layout and let the activity handle the assignment. – Kevin Coppock Oct 16 '15 at 19:49
  • No problem! :) Good luck – Kevin Coppock Oct 16 '15 at 19:53