How to start an activity from a plain non-activity

2019-08-13 19:16发布

问题:

I want to implement MVVM pattern using data-binding in my app. Here's the login button from my layout file:

<Button
     android:id="@+id/login"
     android:layout_width="0dp"
     android:layout_height="50dp"
     android:text="@{mainViewModel.name}"
     android:textColor="@color/white"
     android:onClick="@{mainViewModel.startNewActivity}"/>

The logic is, on clicking this login button, a new activity should be started. I'm a beginner in android, and my question is should I keep the startActivity part in my MainActivity or in my ViewModel class? (If I want it to be in accordance with the MVVM pattern)

If it should be in the mainActivity, I would replace the last line with android:onClick="startNewActivity" and then define the method in my MainActivity.

But, if it is to be placed in the ViewModel class, declaring the following method:

public void startNewActivity(View view) {
    Intent login = new Intent(MainActivity.class, LoginActivity.class);
    startActivity(login);
}

is giving error:

Cannot resolve constructor 'Intent(java.lang.Class<package.name.MainActivity>,java.lang.Class<package.name.LoginActivity>)'

How do I start the LoginActivity from my ViewModel class then?

回答1:

If you want to start LoginActivity from MainActivity then you have to pass this class in intent on button click event. like this,

MainViewModel

public class MainViewModel extends BaseObservable 
{ 
    public String name; 
    Context con; 
    public MainViewModel(Context context)
    { 
        this.con = context; 
    }  
public void click(View view) 
 { 
   Intent login = new Intent(con, LoginActivity.class); 
   con.startActivity(login); 
  } 
} 

MainActivity.java

public class MainActivity extends AppCompatActivity {

        @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ActivityMainBinding binding = DataBindingUtil.setContentView(this,  R.layout.activity_main);
  MainViewModel mainViewModel = new MainViewModel(MainActivity.this);
  binding.setMainViewModel(mainViewModel);
 }
}

LoginActivity.java

  public class LoginActivity extends Activity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.login);
}
 }

activity_main.xml

     <Button
    android:id="@+id/login"
    android:layout_width="0dp"
    android:layout_hiehgt="20dp"
    android:onClick="@{mainViewModel.click}"/>


回答2:

first argument of Intent will be context, not class. Replace your first argument with MainActivity.this

new Intent(MainActivity.this, LoginActivity.class);


回答3:

change like below.

Intent login = new Intent(MainActivity.this, LoginActivity.class);
startActivity(login);


回答4:

First create new activity(MainActivity).check the following code.

public class MainActivity extends AppCompatActivity {

Button btnLogin;

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

    btnLogin = (Button) findViewById(R.id.loginBtn);

    btnLogin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i = new Intent(MainActivity.this, LoginActivity.class);
            startActivity(i);
        }
    });
}
}

create another activity(LoginActivity). LoginActivity.java

public class LoginActivity extends Activity {

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

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:text="LoginBtn"
    android:id="@+id/loginBtn"
    />
</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gami.activity"
android:versionCode="1"
android:versionName="1.0" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="LoginActivity"
        android:screenOrientation="portrait" >
    </activity>
</application>

</manifest>


回答5:

Well you cannot start Activity from a custom class eg: Employee. All you can do is change the Architecture of your application so that you can start an Activity on some button click in the MainActivity.

and then to start

startActivity(new Intent(MainActivity.this, LoginActivity.class));

where MainActivity.this is the context and LoginActivity.class is that Activity to be started.