Suppose there is a standard class:

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstaceState) { super.onCreate(savedInstaceState); setContentView(R.layout.activity_main); } } 

How to create a general class for activations (Suppose, XMLtemplate.java ), so that depending on the parameter, certain setContentView are loaded into setContentView . For example, when creating an instance of the XMLtemplate class XMLtemplate pass a certain parameter that points to a specific activation (With the main parameter, load R.layout.activity_main , with about -> R.layout.activity_about ):

  XMLtemplate xmltemplate = new XMLtemplate(); xmltemplate.param("main"); 

Not to create a MainActivity or AboutActivity class each time and not to onCreate standard code in the onCreate method.

  • And how are you going to resolve the data processing inside the activit? After all, as I understand it R.layout.activity_main and R.layout.activity_about have different components? And do you want to write code in one activity to handle all cases of markup? - Werder
  • @Werder, I don't have much data. In each activation, the text is only, and through the parameter I will indicate which one to show and download - like the pages in the textbook - Amandi
  • Does it make sense then to make different activites? Maybe you just need to send the text you want to display in the intent? This will simplify the implementation and the amount of code - Werder
  • @Werder, can you give an example? - Amandi

1 answer 1

  • Create one activity, for example MainActivity , with one markup.
  • Then, when calling this activation in Intent transfer the text you need to display as follows:

    Intent intent = new Intent(this, MainActivity.class); intent.putString("key", "some useful text"); startActivity(intent);

  • In the MainActivity itself, MainActivity out this text as follows, and do with it what you need:

    String text = getIntent().getString("key");