简体   繁体   中英

Pass String from an Activity to a Class

I have an app when the user registers as a teacher and chooses the subjects they will teach. After pressing the button for a subject, using a Bottom Sheet Dialog with checkboxes, they choose if they will teach college student, high schoolers etc.. I need to pass some string data from Activity to Class but getIntent() stays red and is telling me that it is "deprecated as of API 15:...". I suspect something is wrong with my code and I not doing it correctly. Thank you in advance!

SubjectChoicesActivity


import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.material.bottomsheet.BottomSheetDialog;

public class SubjectChoicesActivity<Checkbox> extends AppCompatActivity {

    Button btn_back;
    Button physics_btn,


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subject_choices);

        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SubjectChoicesActivity.this, RegisterUserActivity.class));
            }
        });


        physics_btn = findViewById(R.id.fizika_btn);

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

                String subject= "Physics";
                Intent i = new Intent(SubjectChoicesActivity.this, BottomSheet.class);
                i.putExtra("subject", subject);
                startActivity(i);
                BottomSheet bottomSheet = new BottomSheet();
                bottomSheet.show(getSupportFragmentManager(), "TAG");
            }
        });


    }


}

BottomSheet


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import java.util.HashMap;
import java.util.Map;

public class BottomSheet extends BottomSheetDialogFragment {
    public BottomSheet() {

    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bottom_sheet_layout, container, false);



        Intent i = getIntent();
        String subject= i.getStringExtra("subject");



        CheckBox os_checkbox = view.findViewById(R.id.os_checkbox);
        CheckBox ss_checkbox = view.findViewById(R.id.ss_checkbox);
        CheckBox fax_checkbox = view.findViewById(R.id.fax_checkbox);


        Button ok_btn = view.findViewById(R.id.ok_btn);
        ok_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Map<String, Object> Subjects= new HashMap<>();


                if(os_checkbox.isChecked()){
                    Subjects.put(subject.toString() + "_os", "true");
                    com.google.firebase.database.FirebaseDatabase.getInstance().getReference().child("User").child("Instruktor").push().setValue(Subjects);
                    Toast.makeText(getContext(), "1", Toast.LENGTH_LONG).show();
                }
                if(ss_checkbox.isChecked()){
                    Subjects.put(subject.toString() + "_ss", "true");
                    com.google.firebase.database.FirebaseDatabase.getInstance().getReference().child("User").child("Instruktor").push().setValue(Subjects);
                    Toast.makeText(getContext(), "2", Toast.LENGTH_LONG).show();
                }
                if(fax_checkbox.isChecked()){
                    Subjects.put(subject.toString() + "_fax", "true");
                    com.google.firebase.database.FirebaseDatabase.getInstance().getReference().child("User").child("Instruktor").push().setValue(Subjects);
                    Toast.makeText(getContext(), "3", Toast.LENGTH_LONG).show();
                }
                if(!os_checkbox.isChecked() && !ss_checkbox.isChecked() && !fax_checkbox.isChecked()){
                    Toast.makeText(getContext(), "0!", Toast.LENGTH_LONG).show();
                }
            }
        });


        return view;

    }
}

Please don't mind the messy code I have a lot to learn.

You can only start Activity classes with startActivity , but your BottomSheet is a DialogFragment, not an Activity. Remove those lines with the Intent and startActivity .

Fragments use arguments instead of intents for receiving data from their source. Arguments are passed via a Bundle, which is the same class that Intent uses for storing "extras".

public void onClick(View v) {
    Bundle args = Bundle();
    args.putString("subject", "Physics");
    BottomSheet bottomSheet = new BottomSheet();
    bottomSheet.setArguments(args);
    bottomSheet.show(getSupportFragmentManager(), "TAG");
}

Then in your Fragment, you can call getArguments() and then call getString() to get your value.

You might wonder why we don't just put the argument in the Fragment's constructor, since we're constructing it ourself. This is because if Android has to recreate your Fragment, it will only be able to use the empty constructor. If your data is in the arguments , Android can preserve them for the recreated Fragment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM