mobilGelistirici
mobilGelistirici

Reputation: 449

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 ?

Upvotes: 5

Views: 10883

Answers (4)

Jeevan
Jeevan

Reputation: 8772

If context (finalContext) that you are using is referring to ApplicationContext then you need to use the flag. Just add the sentence newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); before finalContext.startActivity(newIntent);

Problem with this approach especially when you are starting something outside of your app for ex: mail client etc, is it will continue to be there in the recent apps stack even after the activity is completed, in case of mail client it continues to be there even after mail has been sent

Upvotes: 0

sabadow
sabadow

Reputation: 5161

If take a look to the Context class reference you could see that Activity is an indirect subclass. So if you use an Activity as Context your code should work.

For example:

public ArizaSatiri(Context context, JSONObject mysql_satiri){ ...}

you can change the call to the ArizaSatiri constructor:

new ArizaSatiri(myActivity, mysql_satiri);

Hope it helps.

Upvotes: 1

pablogupi
pablogupi

Reputation: 774

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
    });

Upvotes: 4

Lawrence Choy
Lawrence Choy

Reputation: 6108

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)

Upvotes: 1

Related Questions