Я хочу иметь возможность войти на страницу входа из созданного мною навигационного ящика. Но, тем не менее, после нажатия на кнопку входа я не могу попасть на страницу входа. Я искал, как другие это делали, и пытался реализовать то же самое, но почему я не смог попасть на свою страницу входа из ящика навигации? Я предполагаю, что ошибка здесь будет l ie с моей MainActivity. Вот оно,
MainActivity
package sg.edu.rp.c346.app4thnewspd;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.SwitchCompat;
import androidx.core.view.GravityCompat;
import androidx.core.view.MenuItemCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.widget.Toolbar;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import sg.edu.rp.c346.app4thnewspd.Model.Articles;
import sg.edu.rp.c346.app4thnewspd.Model.Headlines;
import static sg.edu.rp.c346.app4thnewspd.R.id.fragmentContainer;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
RecyclerView recyclerView;
SwipeRefreshLayout swipeRefreshLayout;
EditText etQuery;
Button btnSearch;
final String API_KEY = "5353a8d609b4415ab6f449f31d46926a";
Adapter adapter;
List<Articles>articles = new ArrayList<>();
private String query;
Toolbar toolbar;
DrawerLayout drawer;
ActionBarDrawerToggle toggle;
private Switch btnSwitch;
private NavigationView nv;
Fragment fragment;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
if (AppCompatDelegate.getDefaultNightMode()==AppCompatDelegate.MODE_NIGHT_YES){
setTheme(R.style.DarkTheme);
}else{
setTheme(R.style.LightTheme);
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer);
toggle = new ActionBarDrawerToggle(this,drawer,toolbar,R.string.open,R.string.close);
drawer.addDrawerListener(toggle);
toggle.setDrawerIndicatorEnabled(true);
toggle.syncState();
nv = (NavigationView)findViewById(R.id.navi_view);
nv.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
/* switch(id)
{
}*/
return true;
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(fragmentContainer,
new SignInFragment()).commit();
nv.setCheckedItem(R.id.signIn);
}
Menu menu = nv.getMenu();
MenuItem menuItem = menu.findItem(R.id.Mode);
View actionView = MenuItemCompat.getActionView(menuItem);
btnSwitch = (Switch) actionView.findViewById(R.id.btnSwitch);
if(AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES){
btnSwitch.setChecked(true);
}
btnSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
restartActivity();
}
else
{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
restartActivity();
}
}
});
swipeRefreshLayout = findViewById(R.id.swipeRefresh);
recyclerView = findViewById(R.id.recyclerView);
etQuery = findViewById(R.id.etQuery);
btnSearch = findViewById(R.id.btnSearch);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
final String country = getCountry();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
retrieveJson("",country,API_KEY);
}
});
retrieveJson("",country, API_KEY);
btnSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!etQuery.getText().toString().equals("")){
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
retrieveJson(etQuery.getText().toString(),country,API_KEY);
}
});
retrieveJson(etQuery.getText().toString(),country,API_KEY);
}else{
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
retrieveJson("",country,API_KEY);
}
});
retrieveJson("",country,API_KEY);
}
}
});
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.signIn:
getSupportFragmentManager().beginTransaction().replace(fragmentContainer,
new SignInFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
if(drawer.isDrawerOpen(GravityCompat.START))
drawer.closeDrawer(GravityCompat.START);
else
super.onBackPressed();
}
private void restartActivity() {
Intent i = new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
finish();
}
private String getCountry() {
Locale locale = Locale.getDefault();
String country = locale.getCountry();
return country.toLowerCase();
}
public void retrieveJson(final String query, final String country, final String apiKey){
swipeRefreshLayout.setRefreshing(true);
Call<Headlines>call;
if(!etQuery.getText().toString().equals("")){
call = ApiClient.getInstance().getApi().getSpecificData(query,apiKey);
}else{
call = ApiClient.getInstance().getApi().getHeadLines(country, apiKey);
}
call.enqueue(new Callback<Headlines>() {
@Override
public void onResponse(Call<Headlines> call, Response<Headlines> response) {
if (response.isSuccessful() && response.body().getArticles() != null){
swipeRefreshLayout.setRefreshing(false);
articles.clear();
articles = response.body().getArticles();
adapter = new Adapter(MainActivity.this,articles);
recyclerView.setAdapter(adapter);
}
}
@Override
public void onFailure(Call<Headlines> call, Throwable t) {
swipeRefreshLayout.setRefreshing(false);
Toast.makeText(MainActivity.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}