Home > front end >  Automaticly running a intent
Automaticly running a intent

Time:12-17

I am working on a project where I have 4 Activity. Main activity contains some categories with recycleView and firebase as a database. Inner activity contains some sub-categories I use recycle view for this. On no3 I have a quiz activity where I put some question and answer then a result activity. In result activity I display a return button. After clicking return button it should go to inner activity. But it is going to quiz activity. It looks like it is going to inner activity but automatically running an intent to quiz activity. Or could be something else.

package ali.edu.topic;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

// Result Activity//

import ali.edu.topic.databinding.ActivityResoultBinding;

public class ResoultActivity extends AppCompatActivity {


    ActivityResoultBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityResoultBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        int currectAns = getIntent().getIntExtra("currectAns",0);
        int totalQuitions = getIntent().getIntExtra("total",0);

        binding.scoor.setText(String.format("%d/%d", currectAns, totalQuitions));

        binding.returnBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ResoultActivity.this, InnerActivity.class);
                startActivity(intent);
            }
        });
    }
}



// Inner Activity

package ali.edu.topic;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;

import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.google.firebase.firestore.QuerySnapshot;

import java.util.ArrayList;

import ali.edu.topic.databinding.ActivityInnerBinding;

public class InnerActivity extends AppCompatActivity {


    ActivityInnerBinding binding;
    FirebaseFirestore database;
    ArrayList<InnerModel> innerModels;
    InAdapter Inadapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityInnerBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        database = FirebaseFirestore.getInstance();

        String name = getIntent().getStringExtra("name");
        binding.hadTitle.setText(name);

        innerModels = new ArrayList<>();
        Inadapter  = new InAdapter(this, innerModels);
        database.collection("topics").document(name).collection("boards").orderBy("number")
                .addSnapshotListener(new EventListener<QuerySnapshot>() {
                    @Override
                    public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
                        innerModels.clear();
                        for (DocumentSnapshot snapshot : value.getDocuments()) {
                            InnerModel model = snapshot.toObject(InnerModel.class);
                            innerModels.add(model);
                        }
                        Inadapter.notifyDataSetChanged();
                    }
                });

        binding.innerRecycleView.setLayoutManager(new LinearLayoutManager(this));
        binding.innerRecycleView.setAdapter(Inadapter);
    }
}

CodePudding user response:

you should to use finish() method when launch Intent in QuizActivity.

  • Here is your Activity Launch Follow, as I understand it
  1. MainActivity
  2. SubCategoryActivity
  3. QuizActivity
  4. ResultActivity

Change it like this where you launch intent in QuizActivity.

startActivity(new Intent(QuizActivity.this, ResultActivity.class));
finish();
  • Related