gpt4 book ai didi

android - 想在我的应用程序中提供适当的导航系统..?

转载 作者:行者123 更新时间:2023-11-30 05:11:50 25 4
gpt4 key购买 nike

我想在应用程序中提供适当的后退导航系统。我列出了应用程序中加载的 Activity 。当我按回它会检查列表并加载列表顶部的 Activity 并从 LIST 中删除元素然后有一个名为 last_id 的变量名称跟踪应用程序中加载的最后一个 ID 如果应用程序中加载了相同的 Activity ,然后它不会添加到 LIST 中。但它不起作用,当我在应用程序中加载相同的 Activity 时,它会记录它并在我按下相同次数后退出。例如:- 如果我在主 Activity 上按下按钮,再次加载 MainActivity 6 次,然后应用程序在按下退出按钮 12 次(一个 Activity 2 次)后退出。但我不想要这个我想要我按下按钮 6 次它不会添加到列表中并在按下退出按钮 2 次(正常退出)后退出。代码为主要 Activity

package com.example.gaurav.phone_get;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
public ImageButton getS;
other_parts op;
private Button b;
static int counter=0;
protected static final int MAIN_ACT_CODE=102;
private boolean exit = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getS =(ImageButton) findViewById(R.id.getS);
op = new other_parts(getApplicationContext());
op.getID(MAIN_ACT_CODE);//calls 1st time otherparts
b=(Button)findViewById(R.id.Main);

b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//ActivityInfo vxd = new ActivityInfo();
//Toast.makeText(MainActivity.this, ""+vxd, Toast.LENGTH_SHORT).show();

++counter;
b.setText(String.valueOf(counter));
Intent i= new Intent(MainActivity.this,MainActivity.class);
startActivity(i);

Log.d("counter ","Increasing the counter ");
}
});
}
public void getStart(View v)
{

load_frag();


}

@Override
public void onBackPressed() {
if(exit)
{
super.onBackPressed();
finish();
return;
}
else
{
if(op.loadback_Activity())
{
this.exit=true;
Log.d("if","*****************in the if condition***********");
}
else {
this.exit = true;
Toast.makeText(this, "Press Again to exit", Toast.LENGTH_SHORT).show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 5000);
}
}
}


@Override
protected void onResume() {
super.onResume();
op = new other_parts(getApplicationContext());
}

@Override
protected void onPause() {
super.onPause();
op = new other_parts(getApplicationContext());
}

@Override
protected void onRestart() {
super.onRestart();
op = new other_parts(getApplicationContext());
}
public void load_frag()
{
getS.setVisibility(View.GONE);
counter_class counterClass = new counter_class();
FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame1, counterClass);
ft.addToBackStack(null);
ft.commit();

}
}

counter_class.java 的代码

包 com.example.gaurav.phone_get;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class counter_class extends Fragment {
protected static final int COUNTER_CLASS_CODE=104;
Button b;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v;
other_parts other_parts= new other_parts(getContext());
v= inflater.inflate(R.layout.counter_layout,container,false);
other_parts.getID(counter_class.COUNTER_CLASS_CODE);

return v;
}
}

other_parts.java 的代码

包 com.example.gaurav.phone_get;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;



public class other_parts {
Context ctx;
static boolean status =false;
other_parts(Context ctx)
{
this.ctx= ctx;
}
private static int id=0;
static int i=0;
public static int last_id=0;
public static List<Integer>curr_act_code =new ArrayList<Integer>();
public void getID(int id)
{
this.id=id;
push_in_current(id);

}
public void push_in_current(int q)
{
if(curr_act_code.isEmpty()) {
Log.d("Empty ","Empty string");
if (last_id == q) {
Log.d("return this", "Returning back");

} else {
curr_act_code.add(q);
last_id = q;
Log.d("Adding","Adding first time");
}

}
else
{
int x= curr_act_code.get(curr_act_code.size()-1);
last_id=x;
if(last_id==id)
{
Log.d("2nd time","..........Returning second time ...........");

}
else
{
curr_act_code.add(q);
last_id=q;
Log.d("Adding ", "Adding second time");
Log.d("current_code_value","current_code_value"+String.valueOf(curr_act_code));
Log.d("lst_id","Last id value"+String.valueOf(last_id));
}

}
Log.d("cuurent's code","..............."+String.valueOf(curr_act_code)+"...........");
}
public boolean loadback_Activity(){

if(!curr_act_code.isEmpty())
{
Log.d("Before deletion", String.valueOf(curr_act_code));
status = true;
int x = curr_act_code.get(curr_act_code.size() - 1);//finds values
int k = curr_act_code.size()-1;//finds index
curr_act_code.remove(k);
Log.d("After deletion",String.valueOf(curr_act_code));
if(curr_act_code.isEmpty()) {
status=false;
}
else
{
Log.d("search activity","Initating the search");
search_activity(x);
}


}
else
{
status =false;
Log.d("false","in the last else");
}
return status;
}
public void search_activity(int x)
{
Log.d("Search ","in the Search");
if(x==102)
{
Intent i= new Intent(ctx,MainActivity.class);
ctx.startActivity(i);
}
else if(x ==104)
{
MainActivity m = new MainActivity();
m.load_frag();
}


}
}

XML 文件是:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout android:id="@+id/frame1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ImageButton
android:layout_width="64dp"
android:layout_height="66dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:onClick="getStart"
android:id="@+id/getS"
android:layout_marginStart="169dp"
android:layout_marginTop="180dp"
android:background="@drawable/round_button"
android:src="@drawable/counter" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Main"/>
</RelativeLayout>

谁能帮帮我......

最佳答案

要在 android 应用程序中提供适当的导航机制,您不需要自己编写代码,因为 android Activities 是在 Stack 中维护的,您只需要实现 ActionBarsetDisplayHomeAsUpEnabled(true) 正确地让那个东西工作。

在这里按照官方文档:https://developer.android.com/training/appbar/

但在您开始实现ActionBar 之前,首先了解BackStack 以及android 如何维护它:https://developer.android.com/guide/components/activities/tasks-and-back-stack

关于android - 想在我的应用程序中提供适当的导航系统..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53595940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com