Orqa o'qni qanday qaytarish mumkin?

Men musiqa haqida Android ilovasini ishlab chiqyapman. Ushbu ilovada ikkita qismim bor: PopFragment e GenresFragment.
Fragment_pop.xml deb nomlangan PopFragmentning xml faylida menda GenresFragmentga qaytib boradigan orqa o'q bilan asboblar paneli bor.
Mening asboblarimning kodi bu:







PopFragmentning PopFragment Java faylida PopActivity deb nomlangan kod bor, lekin u ishlamayapti Ushbu kod bor:

public class PopActivity extends AppCompatActivity implements View.OnClickListener {


public PopActivity() {
   //Required empty public constructor
}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_pop);
    Toolbar toolbar = (Toolbar)findViewById(R.id.toolBar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    ImageButton backBtn = (ImageButton)findViewById(R.id.arrowPop);
}

@Override
public void onClick (View view) {

    Intent i = new Intent();
    switch (view.getId()) {
        case R.id.arrowPop:
            break;
    }
}

Menga yordam bera olasizmi? rahmat

1

6 javoblar

Odatda o'z o'qi belgisini qo'shishga hojat yo'q, asboblar paneli uni siz uchun qo'llashi kerak.

if(shouldShowArrow()) {
   drawerLayout.setDrawerLockMode(LOCK_MODE_LOCKED_CLOSED, GravityCompat.START);
   drawerToggle.setDrawerIndicatorEnabled(false);
   MyActivity.this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {//hamburglar icon
   drawerLayout.setDrawerLockMode(LOCK_MODE_UNLOCKED, GravityCompat.START);
   MyActivity.this.getSupportActionBar().setDisplayHomeAsUpEnabled(false);
   drawerToggle.setDrawerIndicatorEnabled(true);
}
drawerToggle.syncState();

Keyin o'qni chertganingizda nima sodir bo'lishini belgilashingiz mumkin

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    ButterKnife.bind(this);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    drawerToggle = new ActionBarDrawerToggle(MyActivity.this, drawerLayout, toolbar, R.string.open, R.string.close) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            MyActivity.this.supportInvalidateOptionsMenu();
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            MyActivity.this.supportInvalidateOptionsMenu();
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);

    drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //what to do when you click the arrow
        }
    });
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setHomeButtonEnabled(true);
}

@Override
protected void onPostCreate(Bundle bundle) {
    super.onPostCreate(bundle);
    drawerToggle.syncState();
}

@Override
protected void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}
0
qo'shib qo'ydi

Odatda o'z o'qi belgisini qo'shishga hojat yo'q, asboblar paneli uni siz uchun qo'llashi kerak.

if(shouldShowArrow()) {
   drawerLayout.setDrawerLockMode(LOCK_MODE_LOCKED_CLOSED, GravityCompat.START);
   drawerToggle.setDrawerIndicatorEnabled(false);
   MyActivity.this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
} else {//hamburglar icon
   drawerLayout.setDrawerLockMode(LOCK_MODE_UNLOCKED, GravityCompat.START);
   MyActivity.this.getSupportActionBar().setDisplayHomeAsUpEnabled(false);
   drawerToggle.setDrawerIndicatorEnabled(true);
}
drawerToggle.syncState();

Keyin o'qni chertganingizda nima sodir bo'lishini belgilashingiz mumkin

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    ButterKnife.bind(this);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    drawerToggle = new ActionBarDrawerToggle(MyActivity.this, drawerLayout, toolbar, R.string.open, R.string.close) {
        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            MyActivity.this.supportInvalidateOptionsMenu();
        }

        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            MyActivity.this.supportInvalidateOptionsMenu();
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);

    drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           //what to do when you click the arrow
        }
    });
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setHomeButtonEnabled(true);
}

@Override
protected void onPostCreate(Bundle bundle) {
    super.onPostCreate(bundle);
    drawerToggle.syncState();
}

@Override
protected void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}
0
qo'shib qo'ydi

Quyidagi kodni tezkor tuzatish sifatida foydalaning, bunda qaytariladigan tugma taqsimlanadi.

switch (view.getId()) {
    case R.id.arrowPop:
        onBackPressed();
        break;
}

Ushbu kodni ishlatib, asboblar panelidagi o'zingizning orqa tugmasini bosishingiz shart emas, Android uni boshqaradi.

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

Orqa tugmani bosganingizda Android siz uchun onBackPressed() deb nom beradi.

0
qo'shib qo'ydi

Quyidagi kodni tezkor tuzatish sifatida foydalaning, bunda qaytariladigan tugma taqsimlanadi.

switch (view.getId()) {
    case R.id.arrowPop:
        onBackPressed();
        break;
}

Ushbu kodni ishlatib, asboblar panelidagi o'zingizning orqa tugmasini bosishingiz shart emas, Android uni boshqaradi.

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

Orqa tugmani bosganingizda Android siz uchun onBackPressed() deb nom beradi.

0
qo'shib qo'ydi

Quyidagi kabi harakat qilib ko'ring:

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
    inflater.inflate(R.menu.your_menu, menu);
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home: {
            getActivity().onBackPressed();
            break;
        }
    }
    return true;
}
0
qo'shib qo'ydi

Quyidagi kabi harakat qilib ko'ring:

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){
    inflater.inflate(R.menu.your_menu, menu);
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home: {
            getActivity().onBackPressed();
            break;
        }
    }
    return true;
}
0
qo'shib qo'ydi