0

im programming an app to sort numbers and display the sorting process after the input is sorted , a new button will be showen to display the selection sort steps in a new activity

[SelectionSort activity 1

I want the output of the function SelectionSortMethod in SelectionSortclass to be displayed in a new activity activity_Ssteps

SelectionSort.java :

public class SelectionSort extends AppCompatActivity {

    EditText input;
    EditText output;
    Button Ssteps ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selection_sort);
        input=findViewById(R.id.input);
        output=findViewById(R.id.output);
         Ssteps = findViewById(R.id.steps);

        Ssteps.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                Intent s = new Intent(SelectionSort.this, com.example.sorted.Ssteps.class);

                startActivityForResult(s, 1);
            }
        });}

    public void sortButtonPressed(View view){
            String[] numberList = input.getText().toString().split(","); 

            Integer[] numbers = new Integer[numberList.length];
            for (int i = 0; i < numberList.length; i++) {

                numbers[i] = Integer.parseInt(numberList[i]);
            }

            SelectionSortmethod(numbers); 
            output.setText(Arrays.toString(numbers));

// if button "sort " is pressed , the button "view steps "will be displayed
            Ssteps.setVisibility(View.VISIBLE);
        }
    }

    public static void SelectionSortmethod (Integer[]  arr)
    {
        // some code for sorting and showing the steps
}

Ssteps.java :

public class Ssteps extends AppCompatActivity {
    TextView steps_text ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ssteps);
        setTitle("selection sort steps ");
        steps_text =findViewById(R.id.Stepstextview);
    }
}
aphro19
  • 17
  • 4
  • You'll have pass thag data to Ssteps using extras in Intent. See https://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data – ashu Mar 05 '20 at 20:47
  • If it's a large amount of data don't pass it between activities using intents, rather set up a shared view model that way the data isn't passed you just store it in a class that is lifecycle aware meaning you can effectively access it wherever you like – martinseal1987 Mar 05 '20 at 21:50

2 Answers2

1

You can just use intent.extras like so:

   Intent s = new Intent(SelectionSort.this, com.example.sorted.Ssteps.class);
   s.putExtra("AnyID",YOURDATA);
   startActivity(s);

And then in your Ssteps.class you can get the data using the id like this:

String ss = getIntent.getExtra("AnyID"); //the id is the same as the other one above
steps_text.settext(ss);
0

Working with Intents like Youssof described is the way to go for small applications like yours. However as you progress in Android programming, you should definitely have a look at splitting your application in Fragments rather than Activities. They can use a Viewmodel, which makes sharing lots of data between screens much easier. Also Fragments can be use in androidx Navigation component, whos changing Fragments can be beautifully arranged in a UI. Very convenient for product reviews.

Xerusial
  • 525
  • 1
  • 5
  • 17