I study on programming courses. I have two identical activations.

package android.and02.lektion2; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.content.Intent; import android.content.ActivityNotFoundException; public class FirstActivity extends Activity { private String ausgabe=" "; public TextView tvAnzeige; private void addText(String text) { ausgabe+=text; tvAnzeige.setText(ausgabe); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button=(Button)this.findViewById(R.id.second_activity); button.setOnClickListener(new ButtonOnClickListener()); tvAnzeige=(TextView)this.findViewById(R.id.anzeige); addText("FirstActivity- \"onCreate\" durchlaufen\n"); } @Override protected void onStart() { super.onStart(); addText("FirstActivity- \"onStart\" durchlaufen\n"); } @Override protected void onResume() { super.onResume(); addText("FirstActivity- \"onResume\" durchlaufen\n"); } @Override protected void onPause() { super.onPause(); addText("FirstActivity- \"onPause\" durchlaufen\n"); } @Override protected void onRestart() { super.onRestart(); addText("FirstActivity- \"onRestart\" durchlaufen\n"); } @Override protected void onStop() { super.onStop(); addText("FirstActivity- \"onStop\" durchlaufen\n"); } //Innere Listener-Klasse: class ButtonOnClickListener implements OnClickListener { @Override public void onClick(View view) { ausgabe+="Button \"second_activity\" geklickt\n "; Intent intent=new Intent(FirstActivity.this,SecondActivity.class); FirstActivity.this.startActivity(intent); } } 

}

Is it possible to pull out this method

 private void addText(String text) { ausgabe+=text; tvAnzeige.setText(ausgabe); } 

so that both can use it. I tried it myself but I do not have enough knowledge, it gives errors.

 import android.widget.TextView; public class addText extends Object { private String ausgabe=" "; public TextView tvAnzeige; private void addText(String text) { ausgabe+=text; tvAnzeige.setText(ausgabe); } } 
  • But why do you need 2 activations if they are the same? And what does the same mean? - pavel
  • not exactly the same. The first and second switches by pressing the button show the onStart, onPause, etc. cycles. - Tema
  • Class names in java are capitalized. - Real KEK

1 answer 1

I would suggest that they never repeat using OOP.

Create a basic activit:

 public abstract class BaseActivity extends Activity{ private String ausgabe=" "; public TextView tvAnzeige; private void addText(String text){ ausgabe+=text; tvAnzeige.setText(ausgabe); } @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); tvAnzeige=(TextView)this.findViewById(R.id.anzeige); addText(this.getClass().getSimpleName() + " onCreate\" durchlaufen\n"); } } 

And then use something like this:

 public class FirstActivity extend BaseActivity{ //onCreate/onStart... } 

And similarly the second.

  • how this TextView will be initialized, on each activation in the markup it is a different object - pavlofff
  • @pavlofff Well, there is an option already, you can write onCreate as it was, you can take it to the base one as I wrote about. - pavel
  • But in different activations there are usually different markup, otherwise why should they be then made different .. - pavlofff
  • @pavlofff as I understood from the comment - no. And id, in any case, you can use the common or somehow override it in the heir. - pavel
  • The id is assigned when building the project automatically and it will not be the same for different widgets, and independent manipulations with these values ​​will lead to pain and problems. - pavlofff