0

Starting Intent then calling Method? In the two classes, the second one has a StartIntent. Right now it simply starts the intent to the first class. I am wanting to know if it is possible from that same onClickListener to essentially StartIntent for the first class as usual, but then immediately call the defaultMap() method within it.

Sometimes I want to simply start the intent normally, and other times I want to start the intent and then call that method. 1) therefore, I can't just make so that OnCreate of the first class it calls defaultMap, because i don't always want to call it. But also 2) I don't want to JUST call the defaultMap() class. I need to call the full class so that it runs through the onCreate functions THEN goes to the defaultMap

FIRST CLASS USED

public class Daily_Schedule extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_daily__schedule);

    ......
    .......
    ......

}

public void defaultMap(){
    ......
    .......
    ......
}

SECOND CLASS USED

 public class InRouteDisplay extends AppCompatActivity implements OnMapReadyCallback {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_in_route_display);

    home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent myIntent = new Intent(InRouteDisplay.this, DailySchedule.class);
            InRouteDisplay.this.startActivity(myIntent);

        }
    });
    .....
    ....
    .....

}

3 Answers3

0

No. The sending class doesn't get an instance of the Activity to call it on. What you can do is set a parameter in the intent, USE_DEFAULT_MAP to 1. The activity you launch can look for that variable, and use that to know that it should call defaultMap.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

Use if statement inside the Daily_Schedule activity and check the extra whether they are set or null. Use getIntent() method. check the answer of this

From InRouteDisplay activity pass the intent data using putextra before calling InRouteDisplay.this.startActivity(myIntent);

Use this link to how to putextra data to the intent Use this link's answer to know how to putextra data to the intent

Sasadara
  • 26
  • 4
0

Try the following: Two ways: 1) Using putExtra() -------- 2) Using SharedPreferences

1)

Demo4.class:-----------

public class Demo4 extends AppCompatActivity {

private Button b;
private final String CALL_DEFAULT_MAP = "call_default_map";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_daily__schedule);


    if(getIntent() != null) {//1
        if(getIntent().getStringExtra(CALL_DEFAULT_MAP) != null) {
            if (getIntent().getStringExtra(CALL_DEFAULT_MAP).equals("true")) {
                defaultMap();
            }
        }
    }


    b = (Button) findViewById(R.id.b);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent myIntent = new Intent(Demo4.this, Demo5.class);
            finish();
            startActivity(myIntent);

        }
    });

}

public void defaultMap() {
            Toast.makeText(getApplicationContext(),"defaultMap()---called",Toast.LENGTH_LONG).show();
}


}

Demo5.class------

public class Demo5 extends AppCompatActivity {

private Button home;
private final String CALL_DEFAULT_MAP = "call_default_map";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_in_route_display);


    home = (Button) findViewById(R.id.home);

    home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            Intent myIntent = new Intent(Demo5.this, Demo4.class);
            myIntent.putExtra(CALL_DEFAULT_MAP,"true");//1
            finish();
            startActivity(myIntent);

        }
    });

}


} 

2)

Demo4.class---------

public class Demo4 extends AppCompatActivity {

private Button b;
private final String CALL_DEFAULT_MAP = "call_default_map";
private SharedPreferences p;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_daily__schedule);

    p = getApplicationContext().getSharedPreferences("p_key",
            0);//2


    if(p != null){//2
        if(p.getBoolean(CALL_DEFAULT_MAP , false)){
            defaultMap();
        }
    }


    b = (Button) findViewById(R.id.b);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent myIntent = new Intent(Demo4.this, Demo5.class);
            finish();
            startActivity(myIntent);

        }
    });

}

public void defaultMap() {
    setBoolean(CALL_DEFAULT_MAP , false);//2
    Toast.makeText(getApplicationContext(),"defaultMap()---called",Toast.LENGTH_LONG).show();
}

public void setBoolean(String Name, boolean value)
{
    if(p != null){
        SharedPreferences.Editor editor = p.edit();
        editor.putBoolean(Name, value);
        editor.apply();
    }
}

} 

Demo5.class:----------------

public class Demo5 extends AppCompatActivity {

private Button home;
private final String CALL_DEFAULT_MAP = "call_default_map";
private SharedPreferences p;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_in_route_display);

    p = getApplicationContext().getSharedPreferences("p_key",
            0);

    home = (Button) findViewById(R.id.home);

    home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            setBoolean(CALL_DEFAULT_MAP , true);//2
            Intent myIntent = new Intent(Demo5.this, Demo4.class);
                            finish();
            startActivity(myIntent);

        }
    });

}

public void setBoolean(String Name, boolean value)
{
    if(p != null){
    SharedPreferences.Editor editor = p.edit();
    editor.putBoolean(Name, value);
    editor.apply();
    }
}

}
Brainnovo
  • 1,749
  • 2
  • 12
  • 17