views:

81

answers:

4

It seems like every example I can find of switching between Activity's involves creating an Intent and passing in the context of a View via an OnClickListener associated with a button.

But what if you just decide you need to switch Activity's? In my case, a Preference value is causing an Activity switch. How do you create an Intent that can cause an Activity switch without an associated OnClickListener?

+2  A: 
Intent myIntent = new Intent(this, AvitivityName.class);
startActivity(myIntent)

Should do it for you and you can call that from anywhere in your other activity.

Chris Thompson
@chris: is it possible to pass activity name as a String?
mohammad shamsi
@Mohammad Not according to any of the public signatures I've seen, but what you could do is use `Class.forName(fully qualified class name)` to return a `Class` object and then pass that `Class` object to the `Intent` constructor.
Chris Thompson
+2  A: 

Use PreferenceChangeListener :)

Konstantin Burov
A: 

You can create intent in the main activity like this

Intent intent = new Intent(FirstActivity.this, second.class); startActivity(intent);

If you are waiting for result from the second then you should use

StartActivityforresult(intent,request code).

request code can be any integer.

Rakesh Gondaliya
A: 

when ever u want to switch activity . u can call these code .

Intent intent = new Intent(this, MyActivity.class);
startActivity(intent);

You can write this code in PreferenceChangeListener.

Srinivas