简体   繁体   中英

How do you start a new window activity in Android Studio?

I tried several things, such as:

Intent i = new Intent(this, myActivity.class);
startActivity(i)

Where it tells me it cannot resolve symbol 'myActivity'

I tried Context.startActivity and extended the class by Context, at that point it just wants me to implement every single method of Context into my class.

How can I just simply make a new activity visible to the user after an if condition runs into true?

just replace "myActivity" with your new activity name

Intent i = new Intent(CurrentActivity.this, myActivity.class);
startActivity(i)

Here myActivity is the name of your second activity (Which you want to open from this intent).

CurrentActivity is the name of your current activity

First you need to understand how to define a class name(Should always start from a capital letter). Then replace the 'myActivity.class' with your new class name.

Make sure that your activity is defined in the AndroidManifest.xml

 <activity android:name=".YourActivityName" />

if this doesn't fix the problem then try:

Intent myIntent = new Intent(view.getContext(), MyClass.class);

if view is not found, then implement your class with

public class YourClassName extends AppCompatActivity implements View.OnClickListener{@Override
public void onClick(View v) {
   
}}

or if you are in a fragment then:

 Intent myIntent = new Intent(MyFragment.this.getActivity(), MyClass.class);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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