I have two classes which extend Activity and need to call another class method on main class on android development.
I did something like subclass sub = new subclass(). It did not work.
In 1st activity class
package org.me.intent_testing;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
import android.content.Intent;
public class FirstActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Button orderButton = (Button)findViewById(R.id.order);
Button orderButton = new Button(this);
orderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(FirstActivity.this, secondActivity.class);
startActivity(intent);
}
});
}
}
In secondActivity class
package org.me.intent_testing;
import android.app.Activity;
import android.os.Bundle;
import android.widget.*;
import android.view.*;
import android.content.Intent;
public class secondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// setContentView(R.layout.order);
// Button orderButton = (Button) findViewById(R.id.end);
Button orderButton = new Button(this);
orderButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
In AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.me.intent_testing">
<application>
<activity android:name=".FirstActivity" android:label="FirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".secondActivity" />
</application>
</manifest>
In R.java
package org.me.intent_testing;
public final class R {
public static final class attr {
}
public static final class layout {
public static final int main=0x7f020000;
}
public static final class string {
public static final int app_name=0x7f030000;
}
}
I have a problem linking between two classes.
May i know why you would want to do so?
If that method is so important to both of the classes, then create it inside a helper class (Which doesn’t extend Activity) and create an object of it in both the classes, then with that object access that method.