Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Correctly Start Activity from custom View (Java Class)?

I'm trying to define a custom Java class (extending a LinearLayout), which needs to start an activity on click. My code looks like this :

public ArizaSatiri(Context context/*, AttributeSet attrs , final Activity aktivite*/ , JSONObject mysql_satiri)
    {
        super(context/*, attrs*/);

        // code to initialize my view :

        final Context finalContext = context;

        this.setOnClickListener(new OnClickListener() {@Override
            public void onClick(View v) {
                Intent newIntent = new Intent(finalContext, ArizaDetaylari.class);
                finalContext.startActivity(newIntent);
            }//onClick
        });


    }

But when I clicked on the instantiated view, I get the error :

Calling startActivity() from outside of an Activity  context requires the  
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

As you can see from the code, I tried passing the Activity to the constructor, and that worked. But is that the correct way? Which way would you reccomend ?

Edit:

And I also need to call setTypeFace() at some point. Should I use context, or Activity for that ?

like image 816
mobilGelistirici Avatar asked Nov 07 '13 08:11

mobilGelistirici


People also ask

How do you call an activity in Java?

xml file to call the method from the button: Select the button in the Layout Editor. In the Attributes window, locate the onClick property and select sendMessage [MainActivity] from its drop-down list.

What is registerForActivityResult?

registerForActivityResult() takes an ActivityResultContract and an ActivityResultCallback and returns an ActivityResultLauncher which you'll use to launch the other activity. An ActivityResultContract defines the input type needed to produce a result along with the output type of the result.

What is the method start activity () in android?

1.2. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.

How findViewById works?

The findViewById() method is a method of Android's View and Activity classes. The method is used to find an existing view in your XML layout by its android:id attribute. The same can be done for any valid Android View object, such as a Button or a CheckBox view.


2 Answers

Try this:

this.setOnClickListener(new OnClickListener() {@Override
        public void onClick(View v) {
            Intent newIntent = new Intent(finalContext, ArizaDetaylari.class);
            newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            finalContext.startActivity(newIntent);
        }//onClick
    });
like image 77
pablogupi Avatar answered Nov 02 '22 13:11

pablogupi


You can put your code into onAttachedToWindow() of your custom view class.

@Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final Context context = ArizaSatiri.this.getContext();
                Intent intent = new Intent(context , ArizaDetaylari.class);
                context.startActivity(intent);
            }
        });
    }

This helps because this function is called after your view is added to the activity, the view has the reference of it. Constructor of views probably run before being added to the activity, thus the error.

This should also work if you declare your view in xml instead of creating it programmatically. (Not tested yet)

like image 41
Lawrence Choy Avatar answered Nov 02 '22 11:11

Lawrence Choy