Passing object between two Override methods - java

Some basic stuff:
I have two override methods in java:
#Override
protected void onCreate(Bundle savedInstanceState) {
...
Car car = new Car();
}
im creating an object inside
and want to call this object in second #Override
#Override
public void onBackPressed() {
car.drive();
}
How can i pass this object between this two methods ?

Declare the variable outside the onCreate and initialize it inside the onCreate.
Car car = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
car = new Car();
}
#Override
public void onBackPressed() {
if (car != null) {
car.drive();
}
}

Related

Call a method from MainActivity class from a non-activity class

I have troubles on calling the method update from MainActivity class in a the MSG0100 non-activity class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void update(boolean msg100Preselection){
if(msg100Preselection){
mExpandableListViewAdapter.setSelectedChild(-1);
mExpandableListViewAdapter.notifyDataSetChanged();
}
}
}
And this is my class where i want to call the update method of Mainactivity.
public class MSG0100{
boolean msg100Preselection=false;
pulic void onUpdate(){
msg100Preselection=true;
// Want to call my update method here
MainActivity activity= new MainActivity();
activity.update(msg100Preselection); //<-------- Using mainactiviy object crashes my app.
}
}
What you want is impossible as you dont have a pointer to your main activity.
The following statement is invalid.
MainActivity activity= new MainActivity();
You are not allowed to use the new operator to create an activity. That should be done using an intent.
There are several things you could do:
Move your update method in another class
OR
declare your update method as static and use it like this:
MainActivity.update(msg100Preselection);
Try using a callbackListener :-
In your MSG0100 class
public class MSG0100 {
boolean msg100Preselection = false;
private static OnUpdateListener mListener;
public static setListener(OnUpdateListener mListener) {
this.mListener = mListener;
}
public void onUpdate() {
msg100Preselection = true;
if (mListener != null)
mListener.onUpdate(msg100Preselection);
}
public interface OnUpdateListener()
{
void onUpdate ( boolean msg100Preselection);
}
}
In your MainActivity-
public class MainActivity extends AppCompatActivity, OnUpdateListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MSG0100.setListener(this)
}
#Override
public void onUpdate(boolean msg100Preselection) {
if (msg100Preselection) {
mExpandableListViewAdapter.setSelectedChild(-1);
mExpandableListViewAdapter.notifyDataSetChanged();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
MSG0100.setListener(null)
}
}
This way you won't have any memory leaks or crashes due to Activity being killed.

How to pass String value from one void method to another void method

I have two void method in the same class. Method2 get data from textview and it stored in a string. I want to get Method2 string in Method1 string.
Note: It is an android project.
Code is here:
class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
void Method2()
{
//this place i want to get str1 value like this:
// String str2=str1
}
void Method1(View view)
{
String str1=textView.getText().toString();
}
}
try this code
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static String s1 ;
public static String s2 ;
void Method1(View view) {
s1 = textView.getText().toString();
}
void Method2() {
s2 = s1 ;
}
}
Learn and follow Java coding standards. This simple class fails the test.
Method 1 should not return void; it should return the String from the View.
Method 2 should not have an empty parameter list; it should have a String parameter that lets you pass in the value you want.
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void method2(String s) {
//this place i want to get str1 value like this:
// String str2=str1
}
public String method1(View view) {
return textView.getText().toString();
}
}
You don't have an example of how method1 will get a reference to a View.
This implementation will allow you to get the String out of the View and pass it to method2. Is that what you want?
A simple way is to add a parameter to method2()
void method2(String str1) {
//this place i want to get str1 value like this:
// String str2=str1
}
And call it from method1.
Assuming you want both methods to be of void return type, you will need to use a class variable. In your code, str1 is local to Method1, and cannot be accessed from outside Method1. Using a class variable:
class MainActivity extends AppCompatActivity {
String str1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
void Method2() {
String str2 = str1;
}
void Method1(View view) {
str1 = textView.getText().toString();
}
}
Alternatively, if Method2 should be executed after Method1:
class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
void Method2(String str) {
String str2 = str1;
}
void Method1(View view) {
String str1 = textView.getText().toString();
Method2(str1); //pass the string to Method1
}
}

Android / Java - Overriding

I have two Classes "BaseActivity" and "ChildActivity" i.e. ChildActivity inherts BaseActivity.
Question: In my following Code Snippet, whenever i press LEFT BUTTON - it logs me "I am From Child Activity". What would i need to do if i want to call SUPER CLASS functionality by default.
public class BaseActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
};
protected void configureTitleBar(String title) {
ImageButton imgLeftButton = ((ImageButton) findViewById(R.id.actionBarLeftButton));
imgLeftButton.setOnClickListener(BaseActivity.this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.actionBarLeftButton){
printCustomLog("I am From Base");
}
}
}
Child Activity:
public class ChildActivity extends BaseActivity implements OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_child);
configureTitleBar("MyTitle");
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.actionBarLeftButton){
printCustomLog("I am From Child Activity");
}
}
}
If you want to get super class functionality, you can
a) Not Override the onClick() method at all (but I don't think that's what you want)
b) Call super.onClick(v) from onClick() in your child class.
The code in your ChildActivity will then be.
#Override
public void onClick(View v) {
// Check some condition if you want to handle it in Child class
if(condition){
printCustomLog("I am From Child Activity");
}
// Else, as default, call Base class's onClick()
else{
super.onClick(v);
}
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.actionBarLeftButton) {
// here's my work
}
super.onClick(v); // it will call Super's OnClick
}

Android Studio: #Override "Annotations are not allowed here"

I want to implement the ...
#Override
public void onBackPressed() {
}
However, I get an error message saying, "Annotations are not allowed here". I need this method to be implemented here. Is there an alternative?
public class supbreh extends Appbreh
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent_breh);
if (myBundle != null) {
String name = myBundle.getString("workout");
ShowDetails(name);
}
}
private void ShowAbDetails(String mName) {
if(mName.equals("abs1")){
#Override
public void onBackPressed() { //"Not Allowed here"
}
}
void onBackPressed ()
Called when the activity has detected the user's press of the back
key. The default implementation simply finishes the current activity,
but you can override this to do whatever you want.
In here you can't declare this method inside another method .
Only override it in that one Activity
#Override
public void onBackPressed()
{
super.onBackPressed();
}
FYI
#Override
public void onBackPressed() {
Intent intent = new Intent(IndividualAbsWorkout.this, IndividualAbsWorkout.class);
startActivity(intent);
}
You can override onBackPressed as normal and call the method in ShowAbDetails() method like below.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent_breh);
if (myBundle != null) {
String name = myBundle.getString("workout");
ShowDetails(name);
}
}
private void ShowAbDetails(String mName) {
if(mName.equals("abs1")){
onBackPressed();
}
}
#Override
public void onBackPressed() {
// your logic here
}

Is it possible to Call a android class from java class?

I have a one android class which extends Activity.
public class MainAct extends Activity{
Context context;
SourceJava srcClass;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
context = this;
System.out.println("inside oncreate");
setContentView(R.layout.main);
srcClass = new SourceJava();
}
}
I have another Java class as like below and which this is calling from MainAct class.
public class SourceJava {
public SourceJava(){
System.out.println("inside constructor***");
MyClass myClass = new MyClass();
if(myClass != null){
System.out.println("**not null");
myClass.powerOff();
}
}
}
In the SourceJava I am calling another class. i.e
public class MyClass extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
System.out.println("**inside myclass");
powerOff();
}
public void powerOff(){
System.out.println("**inside powerOff");
Intent call = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:5555"));
startActivity(call);
}
}
}
I am getting NullPointerException on line startActivity(call),
myClass.powerOff(); and
srcClass = new SourceJava();
what is the problem with this code?
You are treating MyClass (which is an Activity class) as regular Java class. The MyClass activity is neither registered nor loaded so call to startActivity is pointless here. To do so you need to pass reference of Context to the SourceClass and MyClass via constructors.

Categories

Resources