Irfan Akram
Irfan Akram

Reputation: 29

How to call Intent in a static parameterized method and start new activity

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

Upvotes: 1

Views: 60

Answers (2)

Ted Hopp
Ted Hopp

Reputation: 234857

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

Upvotes: 1

Faiizii Awan
Faiizii Awan

Reputation: 1720

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

Upvotes: 1

Related Questions