Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

How can I call android Activity without startAcitivity(intent)?

In the main page of my android app I've added OptionsMenu. What i want is when the option "ContactUs" is selected a new Activity called "ContactUs" should get called.

public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (item.getTitle().toString().equals("ContactUs")) {
    ContactUs contact = new ContactUs(Home.this);
}
return true;
}

And when the ContactUs activity is opened its layout should be displayed:

public class ContactUs extends Activity{
private Context CONTEXT;

public CustomEqualizer(Context c){
    this.CONTEXT = c;
    setContentView(R.layout.contactus);
}
}

But the problem is the layout will not be displayed until onCreate method is called. My question is how can I call ContactUs option from Home without startActivity?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

startActivity() is the only way to start an Activity. The problem you're having is that you're trying to call setContentView() in the constructor which won't work anyway since it hasn't gotten the application context yet. You override the onCreate() method and call it from there.

-= update =-

You can pass whatever you want between activities by adding it to the Intent's bundle.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...