android活动重启自己

时间:2017-05-14 01:14:03

标签: android firebase android-activity firebase-realtime-database firebase-authentication

我正在使用Firebase处理任务应用。我在删除(并撤消删除操作)方面遇到了问题。

我有以下活动:

  • TaskList - 显示任务列表
  • TaskView - 任务的详细视图(从TaskList中的recyclerView启动)
  • CompletedTask - 在您完成任务时启动以播放整洁动画的活动(从TaskView启动)

CompletedTask启动TaskView时,我使用taskReference.removeValue()删除值条目,然后通过intent(uid, title, dueDate等)传递该任务所需的所有数据..一旦CompletedTask启动,我就会创建一个撤消按钮,以防用户想要撤消。

因此,当在CompletedTask活动中时,该条目已被删除,如果用户需要,我们需要将所有数据放回去。

问题

当我按下撤消按钮时,它应该将TaskItem放回数据库并startActivity返回到任务列表但是所有发生的事情都是每次按下撤消时,completedTask活动都会重新启动,数据库条目会被放回并立即再次删除。

我已尝试过各种调试来尝试找到问题区域,但我已经找到了问题。我已经发布了下面的CompletedTask活动,因为这是错误发生的地方(我想!)

非常感谢任何帮助。

谢谢!

ActivityCompletedTask:

private String uid, title, subject, subjectColor;
private long dateDue;
private DatabaseReference mTaskRef;
private FirebaseAuth mAuth;
private FirebaseUser mUser;
private TaskItem taskItem;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_completed_task);

    //set status bar color
    Window window = this.getWindow();
    window.setStatusBarColor(Color.parseColor("#42a566"));

    //create task item and put it back in the db
    Intent data = getIntent();

    //get data passed from intent
    uid = data.getStringExtra("uid");
    title = data.getStringExtra("title");
    dateDue = data.getLongExtra("dateDue", 1);
    subject = data.getStringExtra("subject");
    subjectColor = data.getStringExtra("subjectColor");

    //create task item
    taskItem = new TaskItem(title, dateDue, subject, subjectColor);
    taskItem.setUid(uid);

    //firebase connection with task ID
    mAuth = FirebaseAuth.getInstance();
    mUser = mAuth.getCurrentUser();
    mTaskRef = FirebaseDatabase.getInstance().getReference()
            .child("tasks")
            .child(mUser.getUid())
            .child(taskItem.getUid());

    }

    //undo clicked
    public void undoComplete(View view){

        //push item back into db
        mTaskRef.setValue(taskItem);

        finishActivity();
    }

    //go back to task list
    private void finishActivity(){

        startActivity(new Intent(ActivityCompletedTask.this, ActivityTaskList.class));


    }

修改

添加代码段,以便CompletedTaskTaskView启动 public void CompleteTask(final View view) { mTaskRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()){ TaskItem taskItem = dataSnapshot.getValue(TaskItem.class); myUid = taskItem.getUid(); myTitle = taskItem.getTitle(); myDateDue = taskItem.getDuedate(); mySubjectName = taskItem.getSubjectName(); mySubjectColor = taskItem.getSubjectPrimaryColor(); //remove value mTaskRef.removeValue(); iCompletedAThing(); } } @Override public void onCancelled(DatabaseError databaseError) { System.out.println("The read failed: " + databaseError.getCode()); } }); } private void iCompletedAThing(){ Intent intent = new Intent(ActivityViewTask.this, ActivityCompletedTask.class); //put all myTask item data into intent intent.putExtra("uid", mTaskKey); intent.putExtra("title", myTitle); intent.putExtra("dateDue", myDateDue); intent.putExtra("subject", mySubjectName); intent.putExtra("subjectColor", mySubjectColor); startActivity(intent); } ,以防出现问题。

 public static bool Print(string filepath, string printerName)
    {
        try
        {
            var doc = new PdfDocument();
            doc.LoadFromFile(filepath);
            doc.PrinterName = printerName;

            var psize = new PaperSize("Custom Paper Size", 417, 1007);
            doc.PrintDocument.DefaultPageSettings.PaperSize = psize;
            doc.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle180;

            doc.PrintDocument.Print();
            doc.Dispose();

            return true;
        }
        catch (Exception ex)
        {
            // More code ...
        }
    }

0 个答案:

没有答案