简体   繁体   English

在Android中启动Intent后立即调用方法?

[英]Calling a method immediately after starting intent in android?

Starting Intent then calling Method? 启动Intent然后调用Method? In the two classes, the second one has a StartIntent. 在这两个类中,第二个类具有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. 我想知道是否有可能像平常一样从相同的onClickListener到本质上是第一个类的StartIntent,但是然后立即在其中调用defaultMap()方法。

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. 1)因此,我不能仅仅让第一个类的OnCreate调用defaultMap,因为我并不总是想要调用它。 But also 2) I don't want to JUST call the defaultMap() class. 还有2)我不想只调用defaultMap()类。 I need to call the full class so that it runs through the onCreate functions THEN goes to the defaultMap 我需要调用完整的类,以便它通过onCreate函数运行,然后转到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);

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

}

No. The sending class doesn't get an instance of the Activity to call it on. 否。发送类没有获取要调用的Activity实例。 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. 您可以做的是将意图中的参数USE_DEFAULT_MAP设置为1。您启动的活动可以查找该变量,并使用该变量知道它应该调用defaultMap。

Use if statement inside the Daily_Schedule activity and check the extra whether they are set or null. 在Daily_Schedule活动中使用if语句,并额外检查它们是否已设置或为null。 Use getIntent() method. 使用getIntent()方法。 check the answer of this 检查这个的答案

From InRouteDisplay activity pass the intent data using putextra before calling InRouteDisplay.this.startActivity(myIntent); 在调用InRouteDisplay.this.startActivity(myIntent);之前,从InRouteDisplay活动使用putextra传递意图数据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 使用此链接可以将数据放入意图中使用此链接的答案可以知道如何将数据放入意图中

Try the following: Two ways: 1) Using putExtra() -------- 2) Using SharedPreferences 请尝试以下方法:两种方式:1)使用putExtra()-------- 2)使用SharedPreferences

1) 1)

Demo4.class:----------- 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------ 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) 2)

Demo4.class--------- 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:---------------- 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();
    }
}

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM