Pages

Monday 19 March 2012

Call activity from class


In android I have faced another problem which is nothing but how to call activity from class.Usually we came across with calling activity from one activity to another activity and calling class from activity. But when I came in that situation to call an activity from class I struggled a lot to solve it.

Hope the following is the code for call an activity from class

Intent spreadSheetIntent = new Intent(con,
                              SpreadSheetActivity.class);
                  con.startActivity(spreadSheetIntent);

In the above example, You have to create Intent instance as normal like calling an activity from another activity.But the thing is the first parameter should be the context  of your application. If you are giving the first parameter as currentclassname.this which means that context of that corresponding class.

Where second parameter is an activity that you want to display/ want to navigate.
Here con is nothing but context of the application. I made con as global.Once my application is started , I assigned my application context to con. So wherever i want to use my application context I can con.

And finally start your activity as normal as using intent J

No comments:

Post a Comment