1

I want to call Intent in a static parameterized method and wants to start new activity from there. I'm using a mathod call named as zodiacSign in my MainActivity with two parameters, it works and throw a call to another activity HoroscopeFinder. During its working I want to open a new activity form HoroscopeFinder. If someone knows please help. my code is given below:

MainActivity Code

 dateSetListener=new DatePickerDialog.OnDateSetListener(){ @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { //code month=month+1; String date= dayOfMonth+"/"+month+"/"+year; datePicker.setText(date); //from here we can get day and month String getDay= String.valueOf(dayOfMonth); getDayInt=Integer.parseInt(getDay); String getMonth= String.valueOf(month); getMonthInt=Integer.parseInt(getMonth); //method call for deduct sign using DOB zodiacSign(getDayInt, getMonthInt);} 

HoroscopeFinder class code

public class HoroscopeFinder { static String astroSign=""; public static void zodiacSign(int day, int month) { //Toast.makeText(, "here done", Toast.LENGTH_LONG).show(); //Log.i("check", "done here...!"); if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19)) { astroSign="Capricorn"; } else if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 17)) { astroSign="Aquarius"; //astro_sign="Aquarius"; } else if ((month == 2 && day >= 18 && day <= 29) || (month == 3 && day >= 1 && day <= 19)) { astroSign="Pisces"; } else ((month == 3 && day >= 20 && day <= 31) || (month == 4 && day >= 1 && day <= 19)) { astroSign="Aries"; Intent intent=new Intent(this, AriesActivity.class); startActivity(intent); } 

2 Answers 2

1

Update your zodiacSign method as

 ........ ........ public static void zodiacSign(int day, int month, Context c) { if (...) { .... ... Intent intent=new Intent(c, AriesActivity.class); c.startActivity(intent); } } 

And then in MainActivity pass context to your zodiacSign method as

... zodiacSign(getDayInt, getMonthInt, MainActivity.this); // this context will help you there to start a new activity 
Sign up to request clarification or add additional context in comments.

Comments

1

You can pass an Activity to zodiacSign so it can construct and start the intent:

public static void zodiacSign(int day, int month, Activity activity) { if (...) { ... Intent intent=new Intent(activity, AriesActivity.class); activity.startActivity(intent); } } 

Then, back in your MainActivity class:

... zodiacSign(getDayInt, getMonthInt, MainActivity.this); 

5 Comments

I think you only need a Context. You do not need an activity.
@doubleA - True, but then if it's passed a Context that is not an Activity, then the intent must include the Intent.FLAG_ACTIVITY_NEW_TASK launch flag and the activity will run in a new task. If you really want to accept any Context, you need to account for that possibility.
@doubleA i got your point but still didn't know How and which context i would have to pass in my function call?
@IrfanAkram - Pass the activity that is calling the zodiacSign method. The effect would be the same as if you called startActivity() from the main activity itself.
@IrfanAkram check out second answer. It is as intended.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.