There are three activites, when you click on the button in the first one, the value of the counter "1" is displayed in the field, the transition to the second Activiti also occurs, to which this value needs to be transferred (that is, "1" should be displayed in the second activit) press the button in the second activation, then in the third activation, the two should be displayed, etc. ... How to do it? Thank you in advance!

Tab1:

public class Tab1 extends Activity { ImageButton button1; int count = 0; TextView t; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab1); button1 = (ImageButton)findViewById(R.id.button2); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent firstIntent = new Intent(Tab1.this, Tab2.class); startActivity(firstIntent); TextView t = (TextView)findViewById(R.id.t); t.setText("score: " + ++count +"/18"); finish(); } }); } } 

Tab2:

  public class Tab1 extends Activity { ImageButton button1; int count = 0; TextView t; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab2); button1 = (ImageButton)findViewById(R.id.button2); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent firstIntent = new Intent(Tab1.this, Tab2.class); startActivity(firstIntent); TextView t = (TextView)findViewById(R.id.t); t.setText("score: " + ++count +"/18"); finish(); } }); } } 

Tab3 looks the same.

    5 answers 5

    Between 2 activations, you can transfer some data using the intent.putExtra () method.
    Something can be seen here and here.

    In the very first activation, the counter value is count = 1. When you click and draw. Further, this value is passed to the second activation:

     intent.putExtra("COUNT", cout); 

    In the second activation, we get the value of the counter from the previous activation value:

     Intent intent = getIntent(); int count2 = intent.getIntExtra("COUNT"); 
    • Thanks for the answers, I read everything on the indicated links and even tried it; the problem is that it describes how to work with EditTExt, and not with the counter. I cannot transfer the value and continue the counting in the next Activity .. - bosenko
    • And what's the problem to transfer that same unit to the next activation and, when pressed, output 2? Your counter is just an interstate variable - and transfer it between activations, making an increment where necessary - Stas0n
    • Forgive my folly, but could you write an increment line, please? - bosenko
    • updated the answer - Stas0n

    Lesson 28. Extras - transfer data using Intent

    • Thanks for the answer, I read everything at the indicated link and even tried it, the problem is that it describes how to work with EditTExt, and not with the counter. I cannot transfer the value and continue the counting in the next Activity .. - bosenko

    Make a class with a static variable. The decision is stupid but still

    • @psyhitus - The decision is stupid and very unfortunate. Bad practice. - Opalosolo
    • Why so? if you need some large object to store. That we do Singleton, Registry and store in it. Anything faster than just taking and passing a pointer to a file of some kind. And in each asset, read it again and again. - psyhitus
    • Because the person will learn bad, and then someone behind him will have to clean up. - geek
    • You need to know all the features of the language to be able to make the right decision. And you drive a person into the frame! Also tell goto to use sin. - psyhitus
    • The normal solution, only if we are in Android, then there is a global context - Application. You can create a class inheriting Application and store the data there. Of course there are pitfalls associated with closing the application and reopening, but they are quite easily solved. Packing data in the Bundle and transferring it to the Intent is nice for small and simple objects, but when it comes to something big, this package-unpacking beats performance. - Gleb Kuznetsov

    Send via intent, like String

     intent.putExtra("COUNT", cout); 

    In the second activation, we get the value of the counter from the previous activation value:

     Intent intent = getIntent(); int count2 = intent.getIntExtra("COUNT", count2); 

      Perhaps startActivityForResult(); something can help you.

      Here in more detail, just 3 activites: tyk

      Another option is to use a Bundle - this type is used in the intent , but in turn, a separate Bundle can exist without an intent