Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use intent in non activity class

I have some little problem with android. Just wanted to know how to call intents from an Adapter Class which just extends BaseAdapter and not Activity Class.

like image 822
Venkat Avatar asked Jun 16 '11 05:06

Venkat


People also ask

How can I get Intent data from another activity?

Retrieving data from intent Once you start the activity, You'll be able to get the data attached in the next activity [or services, BoradCast receivers.. etc] being started. to retrieve the data, we use the method getExtra() on the NextActivity from the intent.

Is Intent an activity?

An intent is to perform an action on the screen. It is mostly used to start activity, send broadcast receiver,start services and send message between two activities. There are two intents available in android as Implicit Intents and Explicit Intents. Here is a sample example to start new activity with old activity.

How could you pass data between activities without Intent?

This example demonstrate about How to send data from one activity to another in Android without intent. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

To start an activity you need context. Try with the following approach

pass the context parameter to the constructor of the class which extends BaseAdapter.

Example

public class MyAdapter extends BaseAdapter {
Context context;
public MyAdapter(Context context) {
    this.context=context;
    }

To start an activity use the following approach

Intent i = new Intent();
        i.setClassName("com.abc.mypackage", "com.abc.mypackage.NewActivity");
        context.startActivity(i);

or

Intent i = new Intent(context, MainActivity.class);
context.startActivity(i);
like image 73
Sunil Kumar Sahoo Avatar answered Nov 01 '22 17:11

Sunil Kumar Sahoo