Stevie
Stevie

Reputation: 421

Android: Why does it create and activity, then destory it, then create again, at one time

I have an activity that I want to have created when a button is clicked. For some reason android creates this activity, then destory it, and then create it again at once. I used log messages to debug the creating and destroying(onCreate message and onDestroy message):

"2020-05-21 08:48:19.358 28554-28554/com.example.amigo D/TAG: Standings has been created

2020-05-21 08:48:19.400 28554-28554/com.example.amigo D/TAG: Standings has been destroyed.

2020-05-21 08:48:19.639 28554-28554/com.example.amigo D/TAG: Standings has been created "

Why does it create it again instead of not destroying it in the first place?

Code of starting the activity:

        groupAdapter.setOnClickListener(new GroupAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Group group) {
                Intent intent = new Intent(GroupListActivity.this, GroupStandingsActivity.class);
                intent.putExtra(GroupStandingsActivity.EXTRA_GROUP_ID, group.id);
                startActivity(intent);
            }
        });

GroupStandings onCreate:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_standings);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        Log.d("TAG", "Standings has been created");
        //region gets Data
        Intent intent = getIntent();
        setTitle(intent.getStringExtra(EXTRA_GROUP_TITLE));
        groupID = intent.getIntExtra(EXTRA_GROUP_ID, -1);
        //endregion
        //region sets FAB
        FloatingActionButton playGameFABtn = (findViewById(R.id.play_game_button));
        playGameFABtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), TeamAssignmentActivity.class);
                intent.putExtra(TeamAssignmentActivity.EXTRA_GROUP_ID, groupID);
                intent.putExtra(TeamAssignmentActivity.EXTRA_PLAYERS_DETAILS, (Serializable) standings);
                startActivityForResult(intent, RUN_GAME_REQUEST);
            }
        });
        //endregion
        //region sets group end case(can't open)
        if (groupID == -1) {
            Toast.makeText(this, "Couldn't open group", Toast.LENGTH_SHORT).show();
            finish();
        }
        //endregion
        //region sets RecyclerView standings
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.standings_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setHasFixedSize(true);
        //endregion
        //region sets recycler-view background opacity - 180/255
        Drawable background = recyclerView.getBackground();
        background.setAlpha(180);
        //endregion
        //region sets standings adapter
        final GroupStandingsDetailAdapter groupStandingsAdapter = new GroupStandingsDetailAdapter();
        groupStandingsAdapter.setContext(getApplicationContext());
        recyclerView.setAdapter(groupStandingsAdapter);
        //endregion
        //region sets ViewModel
        groupStandingsViewModel = new ViewModelProvider(this, new GroupStandingsViewModelFactory(getApplication(), groupID)).get(GroupStandingsViewModel.class);
        groupStandingsViewModel.getAllStandingsDetail().observe(this, new Observer<List<StandingsDetail>>() {
            @Override
            public void onChanged(List<StandingsDetail> standingsDetails) {
                groupStandingsAdapter.submitList(standingsDetails);
                standings = standingsDetails;
            }
        });
        //endregion
        //TODO: add longClickListener for group standings - editing and removing
        //region sets LongClickListener TODO: add edit options for long click on standings
        groupStandingsAdapter.setOnItemLongClickListener(new GroupStandingsDetailAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(StandingsDetail standingsDetail) {
            }
        });
        //endregion
    }

Upvotes: 0

Views: 48

Answers (1)

C&#244;ng Hải
C&#244;ng Hải

Reputation: 5261

Reason Your Activity is recreated because you setRequestedOrientation. You should fix it in your AndroidManifest by

android:screenOrientation="landscape"

Upvotes: 2

Related Questions